如何在每一行中创建Listview项目+按钮?

时间:2016-11-29 09:28:33

标签: android

我是Android编程新手,这个任务真的需要我的学校项目。请帮助我。

我有字符串数组List - (从csv中检索)

  list = new ArrayList<>(Arrays.asList("111,222,333,444,555,666".split(",")));
  myList.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.cell,list));

结果仅显示arrayList的逐行文本。我想逐行为每个生成的按钮添加按钮以删除点击的行。

请问我该怎么做。感谢您理解我的问题。

3 个答案:

答案 0 :(得分:6)

您必须创建一个包含单个项目的自定义布局xml,然后您将按钮添加到此布局以及任何其他项目。

CustomLayout.Xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > 

<TextView
    android:id="@+id/tvContact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textStyle="bold" /> 

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Call" /> 


</RelativeLayout>

现在,在创建自定义项目布局后,您需要listview来保存所有项目。

MainActivity.xml

.
.
<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
.
.

现在在java文件中,只需使用我们的自定义布局xml设置适配器

.
.
list = new ArrayList<String>(Arrays.asList("111,222,333,444,555,666".split(",")));
listview.setAdapter(new MyCustomAdapter(list, context) );
.
.

自定义适配器类

public class MyCustomAdapter extends BaseAdapter implements ListAdapter { 
private ArrayList<String> list = new ArrayList<String>(); 
private Context context; 

public MyCustomAdapter(ArrayList<String> list, Context context) { 
    this.list = list; 
    this.context = context; 
} 

@Override
public int getCount() { 
    return list.size(); 
} 

@Override
public Object getItem(int pos) { 
    return list.get(pos); 
} 

@Override
public long getItemId(int pos) { 
    return list.get(pos).getId();
    //just return 0 if your list items do not have an Id variable.
} 

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        view = inflater.inflate(R.layout.CustomLayout, null);
    } 

    //Handle TextView and display string from your list
    TextView tvContact= (TextView)view.findViewById(R.id.tvContact); 
    tvContact.setText(list.get(position)); 

    //Handle buttons and add onClickListeners
    Button callbtn= (Button)view.findViewById(R.id.btn);

    callbtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) { 
            //do something

        }
    });
    addBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) { 
            //do something
            notifyDataSetChanged();
            .
        }
    });

    return view; 
} 
}

答案 1 :(得分:1)

我们需要 ListviewActivity 来列出您的数据

SchoolAdapter ,这是一个自定义适配器,可以为每一行充气

activity_listview ,这是ListviewActivity的布局

每个行都需要

view_listview_row

现在创建所有文件,如下所示

ListviewActivity

public class ListviewActivity extends AppCompatActivity {

private ListView mListview;
private ArrayList<String> mArrData;
private SchoolAdapter mAdapter;

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

    mListview = (ListView) findViewById(R.id.listSchool);

    // Set some data to array list
    mArrData = new ArrayList<String>(Arrays.asList("111,222,333,444,555,666".split(",")));

    // Initialize adapter and set adapter to list view
    mAdapter = new SchoolAdapter(ListviewActivity.this, mArrData);
    mListview.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();

}

SchoolAdapter

public class SchoolAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<String> mArrSchoolData;

public SchoolAdapter(Context context, ArrayList arrSchoolData) {
    super();
    mContext = context;
    mArrSchoolData = arrSchoolData;
}

public int getCount() {
    // return the number of records
    return mArrSchoolData.size();
}

// getView method is called for each item of ListView
public View getView(int position, View view, ViewGroup parent) {
    // inflate the layout for each item of listView
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.view_listview_row, parent, false);


    // get the reference of textView and button
    TextView txtSchoolTitle = (TextView) view.findViewById(R.id.txtSchoolTitle);
    Button btnAction = (Button) view.findViewById(R.id.btnAction);

    // Set the title and button name
    txtSchoolTitle.setText(mArrSchoolData.get(position));
    btnAction.setText("Action " + position);

    // Click listener of button
    btnAction.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Logic goes here
        }
    });

    return view;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}}

activity_listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D1FFFF"
android:orientation="vertical">


<ListView
    android:id="@+id/listSchool"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="#0000CC"
    android:dividerHeight="0.1dp"></ListView>

view_listview_row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="7.5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="7.5dp">

<TextView
    android:id="@+id/txtSchoolTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="2dp"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="20dp" />

<Button
    android:id="@+id/btnAction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="Click Me" />

最后但并非最不重要的是,不要忘记在 manifest.xml中添加您的活动

答案 2 :(得分:0)

在另一个文件中创建自定义列表视图,其中包含列表中每个项目的唯一内容。

然后创建一个自定义Adapter扩展BaseAdapter并绑定它。

请参阅此网站。

https://looksok.wordpress.com/tag/listview-item-with-button/

OR

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/