我正在使用自定义列表视图,我想将项目添加到我的列表视图中。但它似乎正在跳过添加项目的所有代码。有人可以告诉我如何调整我的代码来实现这一目标。
提前谢谢。
这是我的主要活动,我在其中调用自定义对话框
public class MainActivity extends ActionBarActivity {
TextView threadId;
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button newMessage = (Button) findViewById(R.id.new_message_button);
final Context context = this;
final ListView listView = (ListView) this.findViewById(R.id.messagingListView);
final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), this.MessageFeedData());
listView.setAdapter(itemAdapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
listView.getAdapter().getItem(position);
MessageItem itemAtPos = (MessageItem) parent.getItemAtPosition(position);
Intent intent = new Intent(MainActivity.this, ConversationView.class);
intent.putExtra("threadId", String.valueOf(itemAtPos.ThreadId));
startActivity(intent);
}
});
newMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.write_message_layout);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
final Button post_button = (Button) dialog.findViewById(R.id.button_post);
final EditText new_write_message = (EditText) dialog.findViewById(R.id.messge_msg);
final EditText to_message = (EditText) dialog.findViewById(R.id.to_newmsg);
post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
items.add(new MessageItem(5458, to_message.getText().toString(), "imh", DateTime.now(), new_write_message.getText().toString()));
itemAdapter.notifyDataSetChanged();
if (v.getId() == R.id.button_post);
to_message.setText("");
new_write_message.setText("");
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
.show();
}
});
dialog.show();
}
});
}
public ArrayList<MessageItem> MessageFeedData() {
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
//recieved_item_click_actions fields
items.add(new MessageItem(1, "Bob Doe", "image", DateTime.now(), "Hello how are you?"));
items.add(new MessageItem(200, "Simon Pink", "image", DateTime.now(), "Hello what are you doing"));
return items;
}
class ActivityFeedTask extends AsyncTask<Integer, Void, Void> {
ArrayList<MessageItem> recentTracks;
@Override
protected Void doInBackground(Integer... page) {
try {
recentTracks = new ArrayList<MessageItem>();
Thread.sleep(3000);
MessageItem data = null;
for (int i = 0; i < 10; i++) {
recentTracks.add(data);
}
} catch (Exception e) {
}
return null;
}
}
public class ActivityAdapter extends ArrayAdapter<MessageItem> {
private final Context context;
private final ArrayList<MessageItem> items;
//private int currentPage = 0;
public ActivityAdapter(Context context, ArrayList<MessageItem> recentTrackArrayList) {
super(context, 0, recentTrackArrayList);
this.context = context;
this.items = recentTrackArrayList;
}
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.message_list_item, parent, false);
rowView = convertView;
TextView comment2 = (TextView) rowView
.findViewById(R.id.messaging_username);
comment2.setText(items.get(position).Username);
ImageView comment3 = (ImageView) rowView
.findViewById(R.id.messaging_photo);
if (items.get(position).Image == null) {
comment3.setImageResource(R.drawable.ic_launcher);
}
TextView comment4 = (TextView) rowView
.findViewById(R.id.messaging_date);
comment4.setText(items.get(position).DateTimeStamp.toString());
TextView comment5 = (TextView) rowView
.findViewById(R.id.messaging_string);
comment5.setText(items.get(position).MessageString);
}
return convertView;
}
}
}
这是我的自定义对话
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/to_newmsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TO" />
<EditText
android:id="@+id/messge_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/to_newmsg"
android:text="MESSAGE" />
<Button
android:id="@+id/button_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/messge_msg"
android:text="Send" />
</RelativeLayout>
答案 0 :(得分:2)
你有这个
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
在onCreate之前
虽然这个
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
MessageFeedData()中的是本地的。
使用单个列表并向其添加项目,然后调用notifyDateSetChanged()
刷新列表视图。
如您所见,您不使用items
作为构造函数的arg
final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), this.MessageFeedData());
编辑:
final ListView listView = (ListView) this.findViewById(R.id.messagingListView);
items = this.MessageFeedData());
final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), items);