我创建了一个显示项目列表(疾病)的程序,我设计的方式是显示左侧显示的图像,然后显示文本。我想要做的是填充数组的图像和文本,称为"疾病"动态地,我设法填充数组但想要帮助如何使用文本填充图像。我想帮助如何设置一个像疾病阵列一样的图像阵列。谢谢
custom_row.xml
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/diabetesImage"
android:src="@drawable/diabetes"
android:layout_margin="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/diseaseTextView"
android:layout_margin="5dp"
android:singleLine="false" />
主文件:HealthActivity.java
public class HealthActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health);
String [] diseases = {"HIV","Tuberclosis","Diabetes",
"Blood Pressure","Liver disease", "Malaria"};
ListAdapter diseaseAdapter = new CustomAdapter(this, diseases);
ListView TopicListView = (ListView) findViewById(R.id.TopicList);
TopicListView.setAdapter(diseaseAdapter);
TopicListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String disease = String.valueOf(parent.getItemAtPosition(position)) ;
Toast.makeText(HealthActivity.this, disease, Toast.LENGTH_LONG ).show();
}
}
);
}
}
帮助类:CustomAdapter.java
class CustomAdapter extends ArrayAdapter<String>{
CustomAdapter(Context context, String [] diseases) {
super(context,R.layout.custom_row, diseases);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater diseaseInflater = LayoutInflater.from(getContext());
View customView = diseaseInflater.inflate(R.layout.custom_row, parent, false);
String singleDiseaseText = getItem(position);
TextView diseaseTextView = (TextView) customView.findViewById(R.id.diseaseTextView);
ImageView diseaseImageView = (ImageView) customView.findViewById(R.id.diabetesImage);
diseaseTextView.setText(singleDiseaseText);
diseaseImageView.setImageResource(R.drawable.diabetes);
return customView;
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
通过这个Android Custom ListView with Images and Text教程,您将能够创建并了解您的目标。
答案 2 :(得分:0)
您应该将自定义arraylist路径指向您的adappter,并将自定义对象中文本的值分配给textview,我认为您应该使用延迟加载来加载图片,我建议您使用Picasso < / p>
答案 3 :(得分:0)
我不会深入说明,因为如果你知道基本面正确的话,这不是一件大事。
youtube上有很多关于它们的例子,你会很高兴。