我目前正在开发一个非常基本的Android应用程序,遇到了一个我自己无法解决的障碍。
在我的应用程序中,我想要一个带有ListView的开始屏幕。在这个ListView的每一行中都应该有一个Button和一个TextView。我想要大约5行。当您单击每个按钮时,您应该能够进入不同的活动。我怎么做?我读了一些关于适配器的东西,但我仍然不确定如何构建它。
这是我的TextView和Button的xml代码:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:id="@+id/rl01">
<TextView
android:layout_width= "wrap_content"
android:layout_height="50dp"
android:id="@+id/text01"
android:text="hello"
android:textSize="24dp"
android:textColor="@color/abc_search_url_text_normal"
android:paddingRight="@dimen/activity_horizontal_margin"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_toRightOf="@id/text01"
android:text="Press Me!"
/>
</RelativeLayout>
答案 0 :(得分:3)
您发布的xml布局将用作每个列表视图项目。
第1步: 创建一个扩展BaseAdapter的类;
public class CustomAdapter extends BaseAdapter
{
Context con;
String[] data;
public CustomAdapter (Context context, String[] data)
{
this.con = context;
this.data = data;
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int position) {
return data[position];
}
@Override
public long getItemId(int position) {
return 0;
}
//this method will be called for every item of your listview
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.customview, parent, false);
TextView text = (TextView) view.findViewById(your text view id); //recognize your view like this
text.setText(data[position]);
return convertView;
}
}
第2步: 在您的活动中,识别您的列表视图:
yourListViewReference = (ListView) findViewById(R.id.your_list_view_id);
并初始化String数组:
String[] data = {"item 1", "item2", "item3"}; //how many items you want
然后创建您创建的自定义适配器的实例,并将其设置为listview:
CustomAdapter ad = new CustomAdapter(this, data);
yourListViewReference.setAdapter(ad);
抱歉我的英语不好。我正在努力。
答案 1 :(得分:0)
来自Vogella的article对你想做的事情非常有用。
基本上,您将创建一个扩展BaseAdapter类的Adapter,如下所示:
public class Adapter extends BaseAdapter {
private List<Item> mItems;
private Context mContext;
public EventAdapter(Context context, List<Event> items) {
mContext = context;
mItems = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
// This recycles your view and prevents constant inflation, which can really hit your performance.
View rowView = convertView;
if (rowView == null) {
ViewHolder viewHolder = new ViewHolder();
rowView = inflater.inflate(R.layout.yourLayout, parent, false);
viewHolder.text = (TextView) rowView.findViewById(R.id.yourTextViewId);
viewHolder.button = (Button) rowView.findViewById(R.id.yourButtonId);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
// Get the correct item by position
Item item = item.get(position);
// Update the row layout with your item data
holder.text.setText(item.text);
holder.button.setButton(item.button);
// Return your row view
return rowView;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int position) {
}
@Override
public long getItemId(int position) {
}
static class ViewHolder {
public TextView text;
public Button button;
}
之后您只需要通过执行
将此适配器设置为ListView或RecyclerViewlistView.setAdapter(new Adapter(this, items));
答案 2 :(得分:0)
我会写一个简单的例子:
你不需要listview行中的按钮,只需实现onItemClick();
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/custom_list"
android:longClickable="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
list_row.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:id="@+id/rl01">
<TextView
android:layout_width= "wrap_content"
android:layout_height="50dp"
android:id="@+id/text01"
android:text="hello"
android:textSize="24dp"
android:textColor="@color/abc_search_url_text_normal"
android:paddingRight="@dimen/activity_horizontal_margin"
/>
MainActivity.java
oncreate()
{
....
lv1 = (ListView) result.findViewById(R.id.custom_list);
String[] listdata = {"txt1", "txt2", "txt3", "txt4", "txt5"};
ListAdapter listAdapt = new ListAdapter(this, listdata );
lv1.setAdapter(listAdapt);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//do ur work here when list row selected
}
});
..
}
ListAdapter.java
public class ListAdapter extends BaseAdapter {
String[] listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context aContext, String[] listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row, null);
TextView tv1 = (TextView)convertView.findViewById(R.id.text01);
tv1.setText(listData.[position]);
return convertView;
}