gpt4 book ai didi

android - 如何按位置从 ListView 获取 View 并将文本设置为项目?

转载 作者:行者123 更新时间:2023-11-30 02:31:42 26 4
gpt4 key购买 nike

我正在尝试实现自定义列表。每行都有一个 Textview,我想通过以下方式将文本设置为 onCreate 中的那些 TextView 但是我得到 NullPointer 异常。

我的自定义列表

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




}

我的主要 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
}

我的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>

实际上,我正在尝试在 scrollView 中使用 EditText 而不是 EditText 实现单行 ListView,以便我有更好的滚动功能方式,并在这样做时遇到各种问题。

因此,如果我有一个精确的示例,其中可以从 Main_activity 中的任何位置访问 EditText(在 ListView 中)并有效地执行 setTet、getText、颜色更改等操作,那将非常有帮助。

最佳答案

如果您想更改颜色和值,请执行ListViewOnItemClick,此处vv 为空,因为您传递的位置是错误的。

在您的自定义适配器中注释掉这一行txtTitle.setText("Menjar Ali"); 因为在此之前您从网络数组中分配了一个值code> 并再次将该值设置为 Menjar Ali。因此,它将为每个行项目显示 Menjar Ali

代码 fragment

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>

适配器

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

如果你想改变 textColor 你可以在任何点击事件中改变它,即点击那个 TextViewOnItemClick ListView

在更改数据的情况下,您必须修改 web 数组中的那些值

关于android - 如何按位置从 ListView 获取 View 并将文本设置为项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27241987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com