我使用新适配器创建了ListView
。连续的每个项目都包含一张照片和一个副标题。当我点击某个项目时,我需要转到另一个类,然后显示详细信息。我使用名为 XMLPeopleParser 的java类生成数据,该类从原始xml
文件中获取已放入数据的数据。
我正在定义一个setOnItemClickListener
但是当点击一个项目时,没有任何反应。
此外,无法添加每行中的照片。
任何帮助将不胜感激!
MainActivity.class
import android.app.Activity;
import android.content.Intent;
import android.provider.Contacts;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class List_Activity extends AppCompatActivity {
private ListView list = null;
private TextView mselection = null;
private ArrayAdapter<String> adapter = null;
private XMLPeopleParser parser = null;
private String [] names = null;
private String [] addresses = null;
private String [] images = null;
private Person [] data = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_);
list = (ListView) findViewById(R.id.listview);
mselection = (TextView) findViewById(R.id.selection);
parser = new XMLPeopleParser(getApplicationContext());
names = parser.getNames();
addresses = parser.getAddresses();
images = parser.getImages();
data = parser.getData();
list.setAdapter(new IconicAdapter(this));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked on " + names[position], Toast.LENGTH_LONG).show();
mselection.setText(names[position]);
}
});
}
private class IconicAdapter extends ArrayAdapter {
Activity context;
IconicAdapter(Activity context) {
super(context, R.layout.list_row, data);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.list_row, null);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
TextView name_title = (TextView) row.findViewById(R.id.name_title);
TextView address_title = (TextView) row.findViewById(R.id.address_title);
name_title.setText(names[position]);
address_title.setText(addresses[position]);
int[] ImageIds = new int[images.length];
String[] ImageNames = new String[images.length];
ImageNames[position] = images[position].substring(0, images[position].indexOf("."));
ImageIds[position] = getResources().getIdentifier(ImageNames[position], "drawable", getPackageName());
Toast.makeText(getBaseContext(), "ImageId is: " + position + " " + ImageNames[position], Toast.LENGTH_SHORT).show();
icon.setImageResource(ImageIds[position]);
return (row);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ww.custom_list_a.List_Activity">
<TextView
android:id="@+id/selection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/selection"
/>
</RelativeLayout>
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageButton
android:id="@+id/icon"
android:src="@android:drawable/btn_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_title"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/address_title"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
XMLPeopleParser.class
public class XMLPeopleParser {
private Person [] data = null;
private Context context = null;
public XMLPeopleParser(Context c){
this.context = c;
// grab the data from the xml
// get the doc parser ready for people.xml
InputStream is = this.context.getResources().openRawResource(R.raw.people);
DocumentBuilder builder = null;
Document doc = null;
try{
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = builder.parse(is);
}catch (Exception e){}
NodeList nameList = doc.getElementsByTagName("name");
NodeList addressList = doc.getElementsByTagName("address");
NodeList dobList = doc.getElementsByTagName("dob");
NodeList colourList = doc.getElementsByTagName("color");
NodeList imageList = doc.getElementsByTagName("image");
NodeList urlList = doc.getElementsByTagName("url");
data = new Person[nameList.getLength()];
for (int i = 0; i < data.length; i++){
String name = nameList.item(i).getFirstChild().getNodeValue();
String address = addressList.item(i).getFirstChild().getNodeValue();
String dob = dobList.item(i).getFirstChild().getNodeValue();
String colour = colourList.item(i).getFirstChild().getNodeValue();
String image = imageList.item(i).getFirstChild().getNodeValue();
String url = urlList.item(i).getFirstChild().getNodeValue();
data[i] = new Person(name, address, dob, colour, image, url);
}
}
public Person [] getData(){return data;}
public Person getData(int i) {return data[i];}
public String[] getNames(){
String[] names = new String[data.length];
for (int i = 0; i < names.length; i ++){
names[i] = getData(i).getName();
}
return names;
}
public String[] getAddresses() {
String[] addresses = new String[data.length];
for(int i = 0; i < data.length; i++)
{
addresses[i] = getData(i).getAddress();
}
return addresses;
}
public String[] getDobs() {
String[] dobs = new String[data.length];
for(int i = 0; i < data.length; i++)
{
dobs[i] = getData(i).getDob();
}
return dobs;
}
public String[] getColours() {
String[] colours = new String[data.length];
for(int i = 0; i < data.length; i++)
{
colours[i] = getData(i).getColour();
}
return colours;
}
public String[] getImages() {
String[] images = new String[data.length];
for(int i = 0; i < data.length; i++)
{
images[i] = getData(i).getImage();
}
return images;
}
public String[] geturls() {
String[] urls = new String[data.length];
for(int i = 0; i < data.length; i++)
{
urls[i] = getData(i).getUrl();
}
return urls;
}
}
R.raw.people
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="people.xsd" generated="2011-11-05T23:09:59">
<person>
<name>ANDY</name>
<address>USA</address>
<color>BLACK</color>
<dob>2020/2/2</dob>
<image>andy.jpeg</image>
<url>https://en.wikipedia.org/wiki/andy</url>
</person>
<person>
<name>Bill</name>
<address>UK</address>
<dob>2020/1/1</dob>
<color>white</color>
<image>bill.jpeg</image>
<url>https://en.wikipedia.org/wiki/bill</url>
</person>
答案 0 :(得分:0)
我认为您的list_row.xml
不正确,因此您的手机可能无法正常工作。你能试试这个。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:src="@android:drawable/btn_star" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/name_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="ABC" />
<TextView
android:id="@+id/address_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="ABC" />
</LinearLayout>
</LinearLayout>
答案 1 :(得分:0)
从listActivity类中删除以下代码:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked on " + names[position], Toast.LENGTH_LONG).show();
mselection.setText(names[position]);
}
});
之后,你的IconicAdapter中的
ImageNames[position] = images[position].substring(0,
images[position].indexOf("."));
ImageIds[position] = getResources().getIdentifier(ImageNames[position], "drawable", getPackageName());
Toast.makeText(getBaseContext(), "ImageId is: " + position + " " + ImageNames[position], Toast.LENGTH_SHORT).show();
icon.setImageResource(ImageIds[position]);
之前:
return (row);
使用此行为每行响应onClick:
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do here whatever you want to do upon click
});