Android 全局变量和自定义适配器

发布于 2024-10-18 07:28:45 字数 3443 浏览 1 评论 0原文

我有一个列表视图,其中两个文本视图作为列。 我正在使用自定义适配器来管理适配器。

public class MyCustomBaseAdapter extends BaseAdapter {
 private static 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;

 }
}

我使用全局变量来更改列表视图的布局。如您所见,txtName 设置为“左侧”文本视图,txtCityState 设置为右侧文本视图。

在活动中,我根据用户对 Button01 或 Button02 的点击将全局变量设置为 0 或 1:

 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(); 

    }
  });

如果我点击按钮,则该变量似乎被设置为 Toast 消息显示 0 或 1。

在我的 MyCustomBaseAdapter 中,我尝试像这样更改布局:

 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);
   }

但没有任何反应。这有什么问题吗?其他一切都工作正常。

将另一个适配器视图与另一个 row.xml 文件一起使用应该更容易。在另一个 row.xml 中,我将为文本视图设置不同的宽度,因此这将是一个简单的解决方案,但 lv1.setAdapter... 行存在问题。

  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));
        }
      });

如您所见,它指的是 MyCustomBaseAdapter2.java。但问题是“构造函数 MyCustomBaseAdapter2(new View.OnClickListener(){}, ArrayList) 未定义”当此处关于 MyCustomBaseAdapter.java 时也会发生这种情况。按钮监听器之外它工作正常。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂归处 2024-10-25 07:28:45

我会以相反的方式处理事情:

在holder中,不要将字段命名为txtNametxtCityState,而是将它们命名为txtLefttxt右

然后,将选择哪个字段进入哪个文本视图的 if 移动到 getView 方法

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;
 }

另一件事:当用户按下其中一个按钮时,调用 Adatper.notifyDataSetChanged()

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()

夢归不見 2024-10-25 07:28:45

看来你的ViewHolder只是用convertView.setTag(holder)存储在convertView内部,但不影响convertView的布局。

影响convertView的唯一一行(至少在显示的代码中)是:

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

之后convertView在任何地方都不会改变。

更新:

View.setTag(object)仅用于存储一些参考数据,并不会改变View的布局。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文