Arrayadapter in Android to Create Simple Listview

ArrayAdapter in android to create simple listview

ArrayAdapter uses a TextView to display each item within it. Behind the scenes, it uses the toString() method of each object that it holds and displays this within the TextView. ArrayAdapter has a number of constructors that can be used and the one that you have used in your example is:

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

By default, ArrayAdapter uses the default TextView to display each item. But if you want, you could create your own TextView and implement any complex design you'd like by extending the TextView class. This would then have to go into the layout for your use. You could reference this in the textViewResourceId field to bind the objects to this view instead of the default.

For your use, I would suggest that you use the constructor:

ArrayAdapter(Context context, int resource, T[] objects). 

In your case, this would be:

ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values)

and it should be fine. This will bind each string to the default TextView display - plain and simple white background.

So to answer your question, you do not have to use the textViewResourceId.

How to set ArrayAdapter to Listview - Android

Change

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item,List<tempList>)

to

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, tempList); 
//...........

REmove list just add tempList

How to Make a Custom ArrayAdapter for ListView

Make a Custom Adapter. Set that adapter to your listview. Your Adapter class should extend ArrayAdapter. In getView() inflate your custom xml with 3 textviews . Use a viewholder for performance.

Hers's an example http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/. Hers's a bit more to listview http://www.youtube.com/watch?v=wDBM6wVEO70.

how to set json parsed data in a listview and then adding search functionality in it. Have a look at the answer. I have used hashmap and displayed data in listview accordingly with search on listview items. Modify the same according to your needs.

how to create a custom array adapter in android studio to disable clicks on list view item


Instead of using a regular ListView use a custom one:

1) Create a CustomListView class:

public class CustomListView extends ListView{

//add these three constructors
public CustomListView(Context context){
super(context);
}
public CustomListView(Context context , AttributeSet attrs){
super(context , attrs);
}
public CustomListView(Context context , AttributeSet attrs, int defStyleAttr){
super(context , attrs, defStyleAttr);
}

//handle the item click
@Override
public boolean performItemClick(View view , int position , long id){

if(!view.isEnabled()){
//don't handle the click
return false;
}else{
//handle the click
return super.performItemClick(view, position, id);
}

}

}
  1. add the CustomListView that you created to your xml layout instead of ListView.

  2. replace:

    ListView listView = findViewById(.......);

    by:

    CustomListView listView = findViewById(.......);

2) remove these methods from your CustomAdapter class:

@Override
public boolean areAllItemsEnabled() {
return false;
}

@Override
public boolean isEnabled(int position) {
// return super.isEnabled(position);
}

3) handle the item click like that:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {

poss = position + 1;
AlertDialog.Builder builder = new AlertDialog.Builder(viewFriends.this);
builder.setTitle("Notice");
builder.setMessage("Please select to to edit, delete a friend or cancel");

// add the buttons
builder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

//.......keep whatever you have the same here

}
});

builder.setNeutralButton(" Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

//disable the view so that you won't receive clicks again
view.setEnabled(false);

}
});

builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();

}
});

If you use the above implementation, then when you click delete button in alert dialog the item won't receive the click event the next time.

listview with arraylist,simple adapter in android

Main.xml

<LinearLayout  

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp" >


<ListView
android:id="@+id/zone_list"
android:layout_marginBottom="70dp"
android:background="@drawable/batteryborder"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

setlanguage.xml

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

<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="18dp"
android:gravity="center_vertical" />

</LinearLayout>

add in onCreate() of your activity file

ListView listView;

String[] from = { "php_key","c_key","android_key","hacking_key" };

ArrayAdapter arrayAdapter;

listView = (ListView) findViewById(R.id.zone_list);

arrayAdapter = new ArrayAdapter<>(this,R.layout.setlanguage, R.id.tvName, from);

listView.setAdapter(arrayAdapter);


Related Topics



Leave a reply



Submit