我在Buttons
中有四 MainActivity
,我想将用户转移到第二个Activity
ListView
,但是我想对每个Button
进行编码,以便将不同的元素转移到ListView
。问题是,如果我使用自定义ListView
适配器或什么????来代码此ListView
答案 0 :(得分:0)
假设您的列表项中有一个文本视图和两个按钮,那么您的自定义适配器将如下所示。 在firstButton上单击打开第一个活动,在第二个活动上打开第二个活动。
public class CustomAdapter extends ArrayAdapter<DataModel> {
private ArrayList<DataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtName;
Button firstButton,secondButton;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.row_item, data);
this.dataSet = data;
this.mContext=context;
}
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
viewHolder.firstButton = (Button) convertView.findViewById(R.id.first_button);
viewHolder.secondButton = (Button) convertView.findViewById(R.id.second_button);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
lastPosition = position;
viewHolder.txtName.setText(dataModel.getName());
viewHolder.secondButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something when the corky is clicked
open second activity
}
});
viewHolder.firstButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something when the corky is clicked
open first activity
}
});
// Return the completed view to render on screen
return convertView;
}
}
答案 1 :(得分:0)
这是我编写的最后一个listAdapter但仍然有第99行的错误是whis.mTitle.setText(titles [j] [k]);
整个代码
public class ListAdapter extends ArrayAdapter<Data> {
private int layoutResource ;
private Activity activity;
private ArrayList<Data> dataList = new ArrayList<>();
Context context;
public ListAdapter(Activity act , int resource , ArrayList<Data> data){
super(act ,resource,data);
layoutResource = resource;
activity = act;
dataList = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
return dataList.size();
}
@Nullable
@Override
public Data getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, final View convertView, ViewGroup viewGroup) {
int buttonPosition,titlePosition;
View row= convertView;
ViewHolder holder = null;
if ( row == null || (row.getTag()) ==null){
LayoutInflater inflater = LayoutInflater.from(activity);
row= inflater.inflate(R.layout.list_row,viewGroup,false);
holder = new ViewHolder();
holder.mTitle = (TextView) row.findViewById(R.id.txtTitle);
holder.mContent =(TextView)row.findViewById(R.id.txtContent);
row.setTag(holder);
}else{holder=(ViewHolder)row.getTag();
}
int [] buttonsName = {R.id.btnAchieveYourLifeGoals,R.id.btnBecomeMoreOptimistic,R.id.btnBecomeWealthyAndSuccess,R.id.btnReduceStress,R.id.btnLeadership};在此输入代码
int [][]titles ={{R.string.createList1, R.string.cleanEnvironment1, R.string.avoidDistractions1, R.string.earlyMorning1
, R.string.useWeekends1, R.string.neverProcrastinate1, R.string.findSupport1, R.string.beFutureOne1, R.string.checkProgress1,
R.string.motivateYourself1},{R.string.treatPeopleWell1, R.string.neverWasteTimeToChangethingsYouCannotChange1,
R.string.expectSomethingBeautiful1, R.string.assumeTheBestOnPeople1, R.string.focusOnTasks1,
R.string.avoidDepressingPeople1, R.string.eatClean1, R.string.avoidTVNews1,
R.string.theBestisnotCome1, R.string.findThingsTobeGrateful1}, {R.string.focusOnEarning1, R.string.stopExchangingTimeAgainstMoney1, R.string.dontPursueSomeoneDream1
, R.string.stepOutYourComfortZone1, R.string.haveGoalsForAlllife1, R.string.findMindedPeople1, R.string.saveFirst1,
R.string.putYourMoneyToWork1, R.string.neverBuyThingsYouCannotAfford1, R.string.believeThatYouWillBeRich1},
{R.string.spenLessTimeWithNegativePeople1, R.string.avoidPeopleWhoCannotControlStress1,
R.string.findQuietPlaces1, R.string.workingManyProjectsIncreasesStress1, R.string.stopWatchingNewsOnTV1,
R.string.acceptProjectsThatYouEnjoy1, R.string.noMultitasking1, R.string.noScreenTime1, R.string.meditation1
, R.string.physicalActivities1},{R.string.visionary1, R.string.strategicActing1, R.string.goalDriven1,
R.string.selfManagement1, R.string.teamBuilding1, R.string.communication1, R.string.accountability1,
R.string.prblemSolver1, R.string.creativity1, R.string.agility1}};
for (int j =0;j<buttonsName.length;j++) {
for (int k = 0;k<titles.length; k++){
holder.mTitle.setText(titles[j][k]);
}
}
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context,Details.class);
i.putExtra("Title" ,dataList.get(position).getTitle());
i.putExtra("Content",dataList.get(position).getContent());
context.startActivity(i);
}
});
return row;
}
class ViewHolder{
TextView mTitle;
TextView mContent;
Data myData;
}
}