Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Listview items + Button in each row?

Tags:

android

I am new to android programming and this task is really need for my school project. Please kindly help me.

I've string array List - (retrieved from csv)

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

The result is showing only line by line text of arrayList. I want to add button to each generated line by line to delete clicked row.

Please how can I do this. Thank you for understanding my problem.

like image 938
Leonar Aung Avatar asked Nov 29 '16 09:11

Leonar Aung


People also ask

How pass data from ListView to another activity in Android?

Implement ListView 's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked. Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.

What is the use of list view?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


2 Answers

You have to create a custom layout xml which having a single item then you will add your button to this layout along with any other items.

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>

Now after creating custom item layout you need listview which holds all items.

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" />
.
.

Now in java file just set adapter with our custom layout xml

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

Custom adapter Class

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; 
} 
}
like image 68
Samarth Sevak Avatar answered Oct 21 '22 00:10

Samarth Sevak


We have need ListviewActivity for listing your data

SchoolAdapter which is custom adapter to inflate each individual row

activity_listview which is layout for ListviewActivity

view_listview_row which is required for each individual row

Now create all file as below

For 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();

}

For 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;
}}

For 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>

For 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" />

At last but not least, do not forgot to add your activity in manifest.xml

like image 2
Harsh Patel Avatar answered Oct 21 '22 01:10

Harsh Patel