我正在创建日历应用程序,以显示日期(如果日期来自json网络), 我想在单击列表中的事件时显示另一个活动中事件的详细信息,但是每次单击该项目时,TextView都会显示项目位置,而不是项目ID
我的OnItemClickListener代码
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*Uri test = Uri.parse("tel:3154431");
Intent intentPhone = new Intent(Intent.ACTION_DIAL, test);
startActivity(intentPhone);*/
Intent i = new Intent(getApplicationContext(), DetailList.class);
i.putExtra("id", id);
startActivity(i);
}
});
我的适配器代码
public class CustomAdapter extends BaseAdapter {
Context exContext;
int list_item;
private final LayoutInflater inflater;
ArrayList<ScheduleInfo> scheduleInfo;
ProdiInfo prodiInfo;
MarketingActionsInfo marketingActionsInfo;
ProdiNameInfo prodiNameInfo;
UniversityInfo universityInfo;
public CustomAdapter(Context mContext) {
this.exContext = mContext;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void updateData(ArrayList<ScheduleInfo> scheduleInfo){
this.scheduleInfo = scheduleInfo;
notifyDataSetChanged();
}
@Override
public int getCount() {
return scheduleInfo.size();
}
@Override
public Object getItem(int position) {
return scheduleInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
TextView action, studyProgram, universityName, time;
convertView = inflater.inflate(R.layout.list_item, parent, false);
action = (TextView) convertView.findViewById(R.id.marketing_action);
studyProgram = (TextView) convertView.findViewById(R.id.studyprogram);
universityName = (TextView) convertView.findViewById(R.id.univname);
time = (TextView) convertView.findViewById(R.id.time);
int id = (scheduleInfo.get(position).getSchedId());
action.setText(scheduleInfo.get(position).getMarketingActionsInfo().getName());
studyProgram.setText(scheduleInfo.get(position).getProdiInfo().getProdiNameInfo().getProdiName());
universityName.setText(scheduleInfo.get(position).getProdiInfo().getUniversityInfo().getUnivName());
time.setText(scheduleInfo.get(position).getStartDate()+" - "+scheduleInfo.get(position).getEndDate());
}
return convertView;
}
}
还有我的DetailActivity代码
public class DetailList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_list);
TextView idTest1 = (TextView)findViewById(R.id.schID);
Intent in = getIntent();
Bundle b = in.getExtras();
if (b!=null){
String j = (String) b.get("id").toString();
idTest1.setText(j);
}
}
}
答案 0 :(得分:1)
无论我们从Adapter的getItemId()返回的内容是什么,我们都将在onItemClick(AdapterView parent,View view,int position,long id)中得到。 就您而言,您正在通过“职位”。因此,您在“ id”中也得到相同的结果。因此,请根据需要进行更改。
@Override
public long getItemId(int position) {
return position;
}
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
答案 1 :(得分:0)
问题是您要发送大量额外邮件,并尝试在另一端获取捆绑包。修改您的代码,如下所示:
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*Uri test = Uri.parse("tel:3154431");
Intent intentPhone = new Intent(Intent.ACTION_DIAL, test);
startActivity(intentPhone);*/
Intent i = new Intent(getApplicationContext(), DetailList.class);
Bundle bundle = new Bundle();
bundle.putLong("id",id);
i.putExtras(bundle);
startActivity(i);
}
});
答案 2 :(得分:0)
您可以在自定义列表视图行中放置一个TextView(例如,该TextView的ID为tvMyId),并将其可见性设置为“消失”。在适配器集ID中填充数据到该textview并按以下方式检索ID
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//add this line
TextView tvMyId = convertTView.findViewById(R.id.tvMyId).
TextView action, studyProgram, universityName, time;
convertView = inflater.inflate(R.layout.list_item, parent, false);
action = (TextView) convertView.findViewById(R.id.marketing_action);
studyProgram = (TextView) convertView.findViewById(R.id.studyprogram);
universityName = (TextView) convertView.findViewById(R.id.univname);
time = (TextView) convertView.findViewById(R.id.time);
// int id = (scheduleInfo.get(position).getSchedId());
tvMyId.setText(scheduleInfo.get(position).getSchedId()); //and add this line
action.setText(scheduleInfo.get(position).getMarketingActionsInfo().getName());
studyProgram.setText(scheduleInfo.get(position).getProdiInfo().getProdiNameInfo().getProdiName());
universityName.setText(scheduleInfo.get(position).getProdiInfo().getUniversityInfo().getUnivName());
time.setText(scheduleInfo.get(position).getStartDate()+" - "+scheduleInfo.get(position).getEndDate());
}
return convertView;
}
ItemClickListener
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = view.findViewById(R.id.tvMyId);
String myId = tv.getString().toString();
Intent i = new Intent(getApplicationContext(), DetailList.class);
i.putExtra("id", myId);
startActivity(i);
}
});
答案 3 :(得分:-1)
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
Intent i = new Intent(getApplicationContext(), DetailList.class);
bundle.putLong("id",id);
i.putExtras(bundle);
startActivity(i)
}
});
并在DetailList.class中获得捆绑包值
try {
Intent in = getIntent();
Bundle b = in.getExtras();
long id= b.getLong("id");
} catch (Exception e) {
e.printStackTrace();
}