似乎无法确定我的适配器的问题。我试图在checkedTextView
下放置一个进度条,以便最大限度地减少元素的数量和相对布局的麻烦(将加载栏放在下面,单选按钮放在{{1}的右侧})。
textView
protected void onCreate(Bundle savedInstanceState)
{
...
connections = new ArrayList<WifiP2pClient>();//make an arraylist for the connections
listView = (ListView)findViewById(R.id.connectionsListView);//initialize the list view
adapter = new P2PClientArrayAdapter(this, android.R.layout.simple_List_item_single_choice, connections);//create an array adapter
listView.setAdapter(adapter);//attach the adapter to the listView
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
...
}
目前,问题是适配器无响应。我使用异步任务更新public class P2PClientArrayAdapter extends ArrayAdapter<WifiP2pClient>
{
private CheckedTextView checkedTextView;
private ProgressBar progressBar;
int lastRadioButtonPressed;
CheckedTextView lastPressedTextView;
public P2PClientArrayAdapter(Context context, int resource, List<WifiP2pClient> clients)
{
super(context, resource, clients);
lastRadioButtonPressed = -1;
lastPressedTextView = null;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
WifiP2pClient client = getItem(position);
if(row == null)
{
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout, parent, false);
checkedTextView = (CheckedTextView)row.findViewById(R.id.clientToString);
progressBar = (ProgressBar)row.findViewById(R.id.clientProgressBar);
row.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(position != lastRadioButtonPressed)
{
if(lastPressedTextView != null)
{
lastPressedTextView.setChecked(false);
}
lastRadioButtonPressed = position;
lastPressedTextView = checkedTextView;
checkedTextView.setChecked(true);
P2PClientArrayAdapter.this.notifyDataSetChanged();
}
}
});
}
checkedTextView.setText(client.toString());
progressBar.setProgress(client.getProgress());
return row;
}
}
并经常调用WifiP2pClient
,但UI中显示的数据永远不会更新。另外,当我按notifyDataSetChanged()
时,没有任何反应(甚至不是正常的黑色抑制动画)。
如果我使用常规的listView
对象,ArrayListAdapter<String>
会正确更新,所以我知道其余的代码正常运行,这个arrayList
有问题。
编辑:我根据这篇文章将arrayAdapter
放在onClickListener()
中:
How to use RadioGroup in ListView custom adapter?
EDIT2:我通过确保getView()
和checkedViewText
对象在视图未为空时被正确更新,我得到适配器以正确响应对其所做的更改以及用户的更改使用progressBar
对象和视图的ViewHolder
和setTag()
功能。我很快就会粘贴这段代码。我仍然没有适配器为getTag()
返回正确的值。
答案 0 :(得分:0)
在做了足够的实验并获得幸运的休息之后,这是我完成的代码(对于未来的googlers和比较的缘故)。
public class P2PClientArrayAdapter extends ArrayAdapter<WifiP2pClient>
{
//The viewholder object isn't neccessary but it allows us to Tag.set()
//with no indicies because we have it wrapped in one big object
private class ViewHolder
{
CheckedTextView checkedTextView;
ProgressBar progressBar;
}
int lastRadioButtonPressed;
CheckedTextView lastPressedTextView;
public P2PClientArrayAdapter(Context context, int resource, List<WifiP2pClient> clients)
{
super(context, resource, clients);
lastRadioButtonPressed = -1;
lastPressedTextView = null;
}
//I wasn't sure how to override ListView's getCheckedItemPosition()
//So I made my own and then changed the calling code.
public int getCheckedItemPosition()
{
return lastRadioButtonPressed;
}
//If you delete an element in your list, you have
//to reset the pointer or you'll have logic errors.
public void resetCheckedItemPosition()
{
lastRadioButtonPressed = -1;
lastPressedTextView = null;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
WifiP2pClient client = getItem(position);
final ViewHolder viewHolder;
if(row == null)
{
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.checkedTextView = (CheckedTextView)row.findViewById(R.id.clientToString);
viewHolder.progressBar = (ProgressBar)row.findViewById(R.id.clientProgressBar);
row.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder)row.getTag();
}
row.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(position != lastRadioButtonPressed)
{
if(lastPressedTextView != null)
{
lastPressedTextView.setChecked(false);
}
lastRadioButtonPressed = position;
lastPressedTextView = viewHolder.checkedTextView;
viewHolder.checkedTextView.setChecked(true);
}
}
});
viewHolder.checkedTextView.setText(client.toString());
viewHolder.progressBar.setProgress(client.getProgress());
return row;
}
}