我怎样才能通过位置得到的ListView视图,设置文本的项目? [英] How can I get view from ListView by position and set text to item?
问题描述
我想实现自定义列表。还有一个TextView每一行,我想设置文本从的onCreate TextView的那些在下面的方法,但是我得到空指针异常。
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.
我的自定义列表
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;
}
}
我的主要活动:
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
}
我list_single.xml
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>
其实,我试图实现与一个EditText,而不是内部滚动型中的EditText单行的ListView让我有滚动倾倒的装备更好的办法,并在这样做,面对各种各样的问题。
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.
所以,如果我有哪里的EditText(里面的ListView)可以从任何地方在Main_activity访问和有效地执行operatiopn像setTet,gettext的,颜色变化等一个precise例子,这将是非常有益的。
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.
推荐答案
如果你想改变颜色和价值实现 OnItemClick
的ListView 的code>这里
VV
为null,因为你是路过该位置是错误的。
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.
在你的自定义适配器
注释掉该行 txtTitle.setText(Menjar阿里);
,因为在此之前,您指定给网络阵列
一个值,你又该值设置为 Menjar阿里
。因此,它会显示 Menjar阿里
的每一行项目。
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段
活动
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>
适配器
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;
}
}
如果你想改变文字颜色
你可以在任何click事件改变它即单击该的TextView
或 OnItemClick
的是的ListView
。
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
这篇关于我怎样才能通过位置得到的ListView视图,设置文本的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!