gpt4 book ai didi

java - 2 行 ListView 未出现

转载 作者:行者123 更新时间:2023-11-29 05:07:47 24 4
gpt4 key购买 nike

我试图在我的 Activity 中创建一个两行 ListView ,但我遇到了一个我不知道如何修复的错误。我怎样才能通过 AppCompat 方式实现这一点?我想在所附的屏幕截图中实现类似的目标。我的错误在第 52 行。

public class WCLineActivity extends ActionBarActivity {

private class Sample {
private CharSequence title;
private CharSequence summary;
private Class<? extends Activity> activityClass;

public Sample(int titleResId, int summaryResId, Class<? extends Activity> activityClass) {
this.activityClass = activityClass;
this.title = getResources().getString(titleResId);
this.summary = getResources().getString(summaryResId);

}

@Override
public String toString() { return title.toString(); }
public String getSummary(){ return summary.toString(); }
}

private static Sample[] mSamples;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wc_line);

// Instantiate the list of samples.
mSamples = new Sample[]{
new Sample(R.string.bank, R.string.zone_1, MainActivity.class),
new Sample(R.string.waterloo, R.string.zone_1, MainActivity.class)
};

setListAdapter(new MyAdapter(this, mSamples));
}

static class MyAdapter extends BaseAdapter {

static class ViewHolder {
TextView title;
TextView summary;
}

LayoutInflater inflater;
Sample[] mSamples;

public MyAdapter(Context contexts, Sample[] samples) {
this.mSamples = samples;
inflater = LayoutInflater.from(contexts);

}

@Override
public int getCount() {
return mSamples.length;
}

@Override
public Object getItem(int position) {
return mSamples[position];
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();

viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_title);
viewHolder.summary = (TextView) convertView.findViewById(R.id.list_item_subtitle);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(mSamples[position].title);
viewHolder.summary.setText(mSamples[position].getSummary());
return convertView;
}
}
}

phone

error

enter image description here

最佳答案

setListAdapterListActivity的一个方法,在Activity中是没有的。要克服它,您可以在布局中声明一个 ListView,使用 findViewById 检索 ListView 并调用 setAdapter,在那个物体上。例如

ListView listView = (ListView) findViewById(R.id.id_list);
listView.setAdapter(new MyAdapter(this, mSamples));

关于java - 2 行 ListView 未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29807096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com