我在使用ArrayList绑定listview时遇到问题。当点击第一个蔬菜类别时,它应该在列表视图中带来不同的3个元素,但只有第一个元素显示在列表视图中,其他元素没有显示。
这是我的代码(我是Android新手的东西)!!!!!!
它应该带这样的列表
problem_list.xml
<?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>
ProblemAdapter.java
package com.packageName;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ProblemAdapter extends BaseAdapter {
private Context context;
private ArrayList<Integer> listID;
private ArrayList<String> English_Title;
private ArrayList<String> Amharic_Title;
private ArrayList<Integer> count;
public ProblemAdapter(Context context, ArrayList<Integer> listID,
ArrayList<String> name,ArrayList<String> Amharic_Title,ArrayList<Integer>
count) {
this.context = context;
this.listID = listID;
this.English_Title = name;
this.Amharic_Title= Amharic_Title;
this.count = count;
}
@Override
public int getCount() {
return English_Title.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
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;
}
}
problem_category.java
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;
}
}
MainActivity click_Listners()
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);
}
});
}
答案 0 :(得分:0)
您没有为创建的新视图设置值。你的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;