开发者

Android global variable and custom adapter

开发者 https://www.devze.com 2023-02-11 21:13 出处:网络
I have a listview with two textviews as columns. I am using a custom adapter to manage the adapter. public class MyCustomBaseAdapter extends BaseAdapter {

I have a listview with two textviews as columns. I am using a custom adapter to manage the adapter.

public class MyCustomBaseAdapter extends BaseAdapter {
 private static开发者_如何学Go ArrayList<SearchResults> searchArrayList;

 private LayoutInflater mInflater;

 public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
  searchArrayList = results;
  mInflater = LayoutInflater.from(context);
 }

 public int getCount() {
  return searchArrayList.size();
 }

 public Object getItem(int position) {
  return searchArrayList.get(position);
 }

 public long getItemId(int position) {
  return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  if (convertView == null) {
   convertView = mInflater.inflate(R.layout.row, null);
   holder = new ViewHolder();

   holder.txtName = (TextView) convertView.findViewById(R.id.left);
   holder.txtCityState = (TextView) convertView.findViewById(R.id.right);

   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }

  holder.txtName.setText(searchArrayList.get(position).getName());
  holder.txtCityState.setText(searchArrayList.get(position).getCityState());

  return convertView;
 }

 static class ViewHolder {
  TextView txtName;
  TextView txtCityState;

 }
}

I using global variables to change the layout of the listview. As you can see txtName is set to the "left" textview and txtCityState is set to the right textview.

In the activity i am setting the globalvariable to 0 or 1 depending on user clicks on Button01 or Button02:

 final Button sortdate = (Button)findViewById(R.id.Button01);
      sortdate.setOnClickListener(new View.OnClickListener() 
  {
    public void onClick(View v) 
    {
    //  lv1.setAdapter(simpleAdapter);
        GlobalVars.setEset(0);  
      //Toast.makeText(MainActivity.this, String.valueOf(GlobalVars.getEset()), Toast.LENGTH_SHORT).show(); 
    }
  });
      final Button sortname = (Button)findViewById(R.id.Button02);
      sortname.setOnClickListener(new View.OnClickListener() 
  {
    public void onClick(View v) 
    {
        GlobalVars.setEset(1);  
      //Toast.makeText(MainActivity.this, String.valueOf(GlobalVars.getEset()), Toast.LENGTH_SHORT).show(); 

    }
  });

The variable seem to be set as the Toast message shows 0 or 1 if i click the buttons.

In my MyCustomBaseAdapter i am trying to change the layout like this:

 if (GlobalVars.getEset() == 0)
   {

   holder.txtName = (TextView) convertView.findViewById(R.id.left);
   holder.txtCityState = (TextView) convertView.findViewById(R.id.right);
   }
   else if (GlobalVars.getEset() == 1)
   {
       holder.txtName = (TextView) convertView.findViewById(R.id.right);
       holder.txtCityState = (TextView) convertView.findViewById(R.id.left);
   }

but nothing happens. What can be wrong with this? Everything else is working fine.

It should be easier to use another adapterview with another row.xml file. In another row.xml I would set different widths for the textviews so this would be an easy solution but there is a problem with the lv1.setAdapter.... line.

  final Button sortname = (Button)findViewById(R.id.Button02);
      sortname.setOnClickListener(new View.OnClickListener() 
      {
        public void onClick(View v) 
        {
            GlobalVars.setEset(1);
            ArrayList<SearchResults> searchResults = GetSearchResults();
            lv1 = (ListView) findViewById(R.id.ListView01);
            lv1.setAdapter(new MyCustomBaseAdapter2(this, searchResults));
        }
      });

As u you see it refers to MyCustomBaseAdapter2.java. But problem is "The constructor MyCustomBaseAdapter2(new View.OnClickListener(){}, ArrayList) is undefined" This happens also when its about MyCustomBaseAdapter.java here. Out of the button listener it works fine.


I would the the things the other way around:

In holder, instead of naming the fields as txtName and txtCityState, call them txtLeft and txtRight.

Then, move the if that selects which field goes into which text view into the getView method

public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  if (convertView == null) {
   convertView = mInflater.inflate(R.layout.row, null);
   holder = new ViewHolder();

   holder.txtLeft = (TextView) convertView.findViewById(R.id.left);
   holder.txtRight = (TextView) convertView.findViewById(R.id.right);

   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }

  String leftText;
  String rightText;

   if (GlobalVars.getEset() == 0)
   {

       leftText  = searchArrayList.get(position).getName();
       rightText = searchArrayList.get(position).getCityState()

   }
   else if (GlobalVars.getEset() == 1)
   {
       rightText = searchArrayList.get(position).getName();
       leftText  = searchArrayList.get(position).getCityState()
   }

  holder.txtLeft.setText(leftText);
  holder.txtRight.setText(rightText);

  return convertView;
 }

Another thing: when the user presses one of those buttons, call Adatper.notifyDataSetChanged()


It seems that your ViewHolder is just stored inside convertView with convertView.setTag(holder), but does not affect the layout of convertView.

The only line (at least in shown code) that affects convertView is:

convertView = mInflater.inflate(R.layout.row, null);

After that convertView is not changed anywhere.

Update:

View.setTag(object) is only used to store some reference data and does not change the layout of the View.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号