Menjar Ali
Menjar Ali

Reputation: 41

How can I get view from ListView by position and set text to item?

I am trying to implement custom list. there is a Textview in each row, I want to set text to those TextView from onCreate in the following way But I get NullPointer exception.

My custom list

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final String[] web;
private final Integer[] imageId;


TextView txtTitle;

public CustomList(Activity context, String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}


@Override
public View getView(int position, View view, ViewGroup parent) {

LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

txtTitle = (TextView) rowView.findViewById(R.id.txt);

txtTitle.setText(web[position]);
txtTitle.setText("Menjar Ali");


imageView.setImageResource(imageId[position]);
return rowView;
}




}

My main activity:

public class MainActivity extends Activity {

ListView list;

  String[] web = {
    "Google Plus",
      "Twitter",
      "Windows"

  } ;

  Integer[] imageId = {
      R.drawable.ic_launcher,
      R.drawable.ic_launcher,
      R.drawable.ic_launcher

  };
TextView myTextView;

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

    CustomList adapter = new CustomList(MainActivity.this, web, imageId);

    list=(ListView)findViewById(R.id.list);

    list.setAdapter(adapter);


    View vv = list.getChildAt(0);

    TextView tt = (TextView)vv.findViewById(R.id.txt);
    tt.setText("momomo");                        //Here I get the error
}

My list_single.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
    <ImageView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="50dp" />
</TableRow>
</TableLayout>

Actually, I am trying to implement a single row ListView with an EditText instead of a EditText inside scrollView so that I have better way of scrolling funtionality, and facing various problem in doing that.

So if I have a precise example where the EditText(inside ListView) can be accessed from anywhere in Main_activity and efficiently perform operatiopn like setTet, getText, color change etc, it would be very helpful.

Upvotes: 1

Views: 3127

Answers (2)

Kaushik
Kaushik

Reputation: 6162

If you want to change color and value implement OnItemClick of ListView here vv is null because the position you are passing that is wrong.

In your custom adapter comment out this line txtTitle.setText("Menjar Ali"); because before that you assigned a value from web array and again you set that value to Menjar Ali. So, it will show Menjar Ali for every row item.

code snippet

Activity

public class MainActivity extends Activity {

ListView list;

  String[] web = {
    "Google Plus",
      "Twitter",
      "Windows"

  } ;

  Integer[] imageId = {
      R.drawable.ic_launcher,
      R.drawable.ic_launcher,
      R.drawable.ic_launcher

  };
TextView myTextView;

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

    CustomList adapter = new CustomList(MainActivity.this, web, imageId);

    list=(ListView)findViewById(R.id.list);

    list.setAdapter(adapter);
}

list_single.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:cropToPadding="false"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="12dp"
        android:text="text" />

</LinearLayout>

Adapter

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final String[] web;
private final Integer[] imageId;


TextView txtTitle;

public CustomList(Activity context, String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}


@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
if(view == null) {
  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  view = inflater.inflate(R.layout.list_single, parent, false);
  viewHolder = new ViewHolder();
  viewHolder.imageView = (ImageView) view.findViewById(R.id.img);
  viewHolder.txtTitle = (TextView) view.findViewById(R.id.txt);
  view.setTag(viewHolder);
} else {
  viewHolder = (ViewHolder) view.getTag();
}

viewHolder.txtTitle.setText(web[position]);
viewHolder.imageView.setImageResource(imageId[position]);
return view;
}

private class ViewHolder {
 TextView txtTitle;
 ImageView imageView;
}
}

If you want to change textColor you can change it in any click event i.e click on that TextView or OnItemClick of that ListView.

In case of changing data you have to modify those values inside web array

Upvotes: 1

VikasGoyal
VikasGoyal

Reputation: 3376

For updating ListView item value you have to set their values in adapter instead of onCreate of activity.

For achieving your target you need to update the list item by updating in object array you provided to list adapter and after that you need to call notifyDataSetChanges for your adapter.

a good tutorials about android list view is define here please have a look for more clarification.

Upvotes: 0

Related Questions