我希望我的listview能够突出显示多行。我查看了所有资源,但似乎没有任何工作 - 我知道我必须使用listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)
;但不太确定我将如何实现这一点。这是我的代码。
public class ConversationView extends Activity {
TextView hello;
Integer threadId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation_view);
Intent intent = getIntent();
String thread = intent.getStringExtra("threadId");
threadId = Integer.parseInt(thread);
final ListView convoListView = (ListView) this.findViewById(R.id.conversationListView);
ArrayList<ConversationItem> convoItems = this.GetItems(threadId);
final ConversionAdapter convoAdapter = new ConversionAdapter(getApplicationContext(), convoItems);
convoListView.setAdapter(convoAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_conversation_view, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public ArrayList<ConversationItem> GetItems(Integer threadId)
{
ArrayList<ConversationItem> convos = new ArrayList<>();
convos.add(new ConversationItem(1, 1, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, "));
convos.add(new ConversationItem(1, 2, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet,Donec lacinia nunc sed faucibus suscipit. Curabitur."));
convos.add(new ConversationItem(1, 3, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, Curabitur."));
convos.add(new ConversationItem(1, 4, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, "));
ArrayList<ConversationItem> returnItems = new ArrayList<>();
for(int i = 0; i < convos.size(); i++)
{
if(convos.get(i).ThreadId.equals(threadId))
{
returnItems.add(convos.get(i));
}
}
return returnItems;
}
这是我的适配器:
public class ConversionAdapter extends ArrayAdapter<ConversationItem>
{
private final Context context;
private final ArrayList<ConversationItem> items;
private int currentPage = 0;
public ConversionAdapter(Context context, ArrayList<ConversationItem> convoItems) {
super(context, 0, convoItems);
this.context = context;
this.items = convoItems;
}
public View getView(int position, View convertView, ViewGroup parent) {
//View rowView;
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = getLayoutInflater().inflate
(R.layout.conversation_list_item, parent,false);
//Username
TextView username = (TextView) convertView
.findViewById(R.id.conversation_username);
username.setText(items.get(position).convoUsername);
//Date Time
TextView dateTime = (TextView) convertView
.findViewById(R.id.conversation_date);
dateTime.setText(items.get(position).DateTimeStamp.toString());
//Message
TextView message = (TextView) convertView
.findViewById(R.id.conversation_message);
message.setText(items.get(position).Message);
}
return convertView;
}
}
}