Android 如何将自定义列表添加到listview控件中?
将listview与ArrayList绑定时出现问题。单击第一个蔬菜类别时,它应在listview中显示不同的3个元素,但只有第一个元素显示在listview上,其他元素没有显示 这是我的代码(我是android新手) 它应该会带来这样的列表 problem\u list.xmlAndroid 如何将自定义列表添加到listview控件中?,android,Android,将listview与ArrayList绑定时出现问题。单击第一个蔬菜类别时,它应在listview中显示不同的3个元素,但只有第一个元素显示在listview上,其他元素没有显示 这是我的代码(我是android新手) 它应该会带来这样的列表 problem\u list.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/ap
<?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="wrap_content">
<ImageView
android:id="@+id/problem_imageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="13dp"
android:layout_marginTop="13dp" />
<!--app:srcCompat="@drawable/pest" />-->
<TextView
android:id="@+id/englishName_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/problem_imageView"
android:layout_marginStart="13dp"
android:layout_toEndOf="@+id/problem_imageView"
android:fontFamily="monospace"
android:text="Category English Name"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<TextView
android:id="@+id/amharicName_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/englishName_textView"
android:layout_below="@+id/englishName_textView"
android:layout_marginTop="4dp"
android:fontFamily="monospace"
android:text="Amharic Name"
android:textColor="#000080"
android:textSize="15sp" />
<TextView
android:id="@+id/count_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/amharicName_textView"
android:layout_below="@+id/amharicName_textView"
android:layout_marginTop="8dp"
android:fontFamily="monospace"
android:text="Count"
android:textColor="#000080"
android:textSize="14sp" />
</RelativeLayout>
您没有为创建的新视图设置值。您的getView主体应该如下所示:
if (convertView == null) {
convertView = View.inflate(context, R.layout.problem_list, null);
} else {
ImageView img = convertView.findViewById(R.id.problem_imageView);
TextView txtView_Eng =
convertView.findViewById(R.id.englishName_textView);
//TextView txtView_Amc =
convertView.findViewById(R.id.amharicName_textView);
TextView txtView_Count =
convertView.findViewById(R.id.count_textView);
img.setImageResource(listID.get(position));
txtView_Eng.setText(English_Title.get(position));
txtView_Amc.setText(Amharic_Title.get(position));
txtView_Count.setText(count.get(position));
}
return convertView;
图像未显示?#John Joe图像显示正确!!对不起,我能帮忙吗?我在这里看到三个项目。什么没有出现?#约翰·乔第二张和第三张名单应该有不同的图像、名称、阿姆哈拉语名称和计数。这就是问题所在。你的问题解决了吗?同样的问题出现了。我是否必须循环并添加列表项??如果在textview中将整数设置为文本,它会将其视为资源id。必须先将整数转换为字符串,然后再将其设置为文本,如下面的txtView_Count.setText(string.valueOf(Count.get(position));谢谢你指出的转换。但是编译器说“从未使用过类ViewHolder”,我如何更正它并获得所需的输出。谢谢删除ViewHolder类。你不需要它。
package com.packageName;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import java.util.ArrayList;
public class problem_category extends AppCompatActivity {
String VegetableCategoryType ;
ListView listView;
ArrayList<Integer> ImageID;
ArrayList<String> English_Title;
ArrayList<String> Amharic_Title;
ArrayList<Integer> count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_problem_category);
// Assigning caller plant to the VegetableCategoryType
Intent intent = getIntent();
VegetableCategoryType = intent.getStringExtra("Plant_Name");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Will be replaced with new action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
listView = findViewById(R.id.category_List_view);
ImageID = new ArrayList<>();
English_Title = new ArrayList<>();
Amharic_Title = new ArrayList<>();
count = new ArrayList<>();
ImageID = getImageID();
English_Title = getEnglishName();
Amharic_Title = getAmharicName();
count = getCount();
ProblemAdapter pAdapter = new ProblemAdapter(problem_category.this,
ImageID,English_Title,Amharic_Title,count);
listView.setAdapter(pAdapter);
}
@Override
public void finish(){
super.finish();
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
}
public ArrayList<String> getEnglishName(){
English_Title= new ArrayList<>();
English_Title.add("Pest");
English_Title.add("Disease");
English_Title.add("Disorder");
return English_Title;
}
private ArrayList<String> getAmharicName() {
Amharic_Title= new ArrayList<>();
Amharic_Title.add("sub-title1");
Amharic_Title.add("sub-title2");
Amharic_Title.add("sub-title3");
return Amharic_Title;
}
public ArrayList<Integer> getImageID(){
ImageID = new ArrayList<>();
ImageID.add(R.drawable.pest);
ImageID.add(R.drawable.disease);
ImageID.add(R.drawable.disorder_edited);
return ImageID;
}
public ArrayList<Integer> getCount(){
count = new ArrayList<>();
if(VegetableCategoryType == "Cabbage"){
count.add(3);
count.add(8);
count.add(3);
}else if (VegetableCategoryType =="Pepper"){
count.add(7);
count.add(5);
count.add(2);
}else if(VegetableCategoryType == "Onion"){
count.add(2);
count.add(5);
count.add(3);
}else if(VegetableCategoryType == "Tomato"){
count.add(8);
count.add(16);
count.add(5);
}
return count;
}
}
public void click_Listeners(){
cabbageImageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent pCategory = new
Intent(getApplicationContext(),problem_category.class);
pCategory.putExtra("Plant_Name","Cabbage");
startActivity(pCategory);
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
}
});
}
if (convertView == null) {
convertView = View.inflate(context, R.layout.problem_list, null);
} else {
ImageView img = convertView.findViewById(R.id.problem_imageView);
TextView txtView_Eng =
convertView.findViewById(R.id.englishName_textView);
//TextView txtView_Amc =
convertView.findViewById(R.id.amharicName_textView);
TextView txtView_Count =
convertView.findViewById(R.id.count_textView);
img.setImageResource(listID.get(position));
txtView_Eng.setText(English_Title.get(position));
txtView_Amc.setText(Amharic_Title.get(position));
txtView_Count.setText(count.get(position));
}
return convertView;