这是我的代码部分的主要部分,我在代码的底部做了一些更改,但是当我在listview中选择一个项目时会出现问题..
任何帮助将不胜感激..
我该如何解决这个问题?
public class DisplayWishesActivity extends Activity {
private DatabaseHandler dba;
private ArrayList<MyWish> dbWishes = new ArrayList<>();
private WishAdapter wishAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_wishes);
listView = (ListView) findViewById(R.id.list);
refreshData();
}
private void refreshData() {
dbWishes.clear();
dba = new DatabaseHandler(getApplicationContext());
ArrayList<MyWish> wishesFromDB = dba.getWishes();
for (int i = 0; i < wishesFromDB.size(); i++){
String title = wishesFromDB.get(i).getTitle();
String dateText = wishesFromDB.get(i).getRecordDate();
String content = wishesFromDB.get(i).getContent();
int mid = wishesFromDB.get(i).getItemId();
MyWish myWish = new MyWish();
myWish.setTitle(title);
myWish.setContent(content);
myWish.setRecordDate(dateText);
myWish.setItemId(mid);
dbWishes.add(myWish);
}
dba.close();
//setup adapter
wishAdapter = new WishAdapter(DisplayWishesActivity.this, R.layout.wish_row, dbWishes);
listView.setAdapter(wishAdapter);
wishAdapter.notifyDataSetChanged();
}
public class WishAdapter extends ArrayAdapter<MyWish>{
Activity activity;
int layoutResource;
MyWish wish;
ArrayList<MyWish> mData = new ArrayList<>();
public WishAdapter(Activity act, int resource, ArrayList<MyWish> data) {
super(act, resource, data);
activity = act;
layoutResource = resource;
mData = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public MyWish getItem(int position) {
return mData.get(position);
}
@Override
public int getPosition(MyWish item) {
return super.getPosition(item);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if ( row == null || (row.getTag()) == null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResource, null);
holder = new ViewHolder();
holder.mTitle = (TextView) row.findViewById(R.id.name);
holder.mDate = (TextView) row.findViewById(R.id.dateText);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.myWish = getItem(position);
holder.mTitle.setText(holder.myWish.getTitle());
holder.mDate.setText(holder.myWish.getRecordDate());
final ViewHolder finalHolder = holder;
// int mypos = position;
// holder.mTitle.setOnClickListener(new View.OnClickListener() {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = finalHolder.myWish.getContent().toString();
String dateText = finalHolder.myWish.getRecordDate().toString();
String title = finalHolder.myWish.getTitle().toString();
int mid = finalHolder.myWish.getItemId();
// Log.v("MyId: " , String.valueOf(mid));
Intent i = new Intent(DisplayWishesActivity.this, WishDetailActivity.class);
i.putExtra("content", text);
i.putExtra("date", dateText);
i.putExtra("title", title);
i.putExtra("id", mid);
startActivity(i);
}
});
return row;
}
class ViewHolder{
MyWish myWish;
TextView mTitle;
int mId;
TextView mContent;
TextView mDate;
}
}
}
答案 0 :(得分:0)
从你的适配器类中删除你的itemclicklistener并使用onItemClick中的位置变量来获取该索引处的MyWish对象,然后通过该对象访问getContent,getRecordDate函数