-5

也许你对我的标题感到困惑。所以我会在这里解释我的问题。我使用自定义布局、适配器创建列表视图活动。但是当我尝试设置 clickItemListListener 时它被强制关闭。请帮助我这是代码

模型.java

package id.akuyMasykur.zakatku.Utilities;

public class Model {
private String counter;
private String title;
private boolean isGroupHeader = false;

public Model(String title){
    this(title, null);
    isGroupHeader = true;
}
public Model(String title, String counter){
    super();
    this.counter = counter;
    this.title = title;
}   
public String getTitle(){
    return title;
}
public void setTitle(String title){
    this.title = title;
}
public String getCounter(){
    return counter;
}
public void setCounter(String counter){
    this.counter = counter;
}
public boolean isGroupHeader(){
    return isGroupHeader;
}
public void setGroupHeader(boolean isGroupHeader){
    this.isGroupHeader = isGroupHeader;
}
}

aAdapter.java

package id.akuyMasykur.zakatku.Utilities;

import id.akuyMasykur.zakatku.R;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class aAdapter extends ArrayAdapter<Model>{

private final Context context;
private final ArrayList<Model> modelsArrayList;

public aAdapter(Context context, ArrayList<Model> modelsArrayList){
    super(context, R.layout.for_item, modelsArrayList);
    this.context = context;
    this.modelsArrayList = modelsArrayList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    //create inflater
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //get rowview from inlflater
    View rowView = null;

    if(!modelsArrayList.get(position).isGroupHeader()){
        rowView = inflater.inflate(R.layout.for_item, parent, false);

        //get icon , title & counter view fro th rowview
        TextView counter = (TextView) rowView.findViewById(R.id.item_icon);
        TextView titleView = (TextView) rowView.findViewById(R.id.item_title);

        //set text for textview
        counter.setText(modelsArrayList.get(position).getCounter());
        titleView.setText(modelsArrayList.get(position).getTitle());

    }else{
        rowView = inflater.inflate(R.layout.header_item, parent, false);
        TextView titleView = (TextView) rowView.findViewById(R.id.header_item);
        titleView.setText(modelsArrayList.get(position).getTitle());
    }

    //return rowview
    return rowView;
}
}

MainActivity.java

package id.akuyMasykur.zakatku;

import id.akuyMasykur.zakatku.Utilities.Model;
import id.akuyMasykur.zakatku.Utilities.aAdapter;   
import java.util.ArrayList; 
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //if extending activity
    setContentView(R.layout.app_header);
    this.getListView().setSelector(R.drawable.list_selector);
    //1. pass context and data to aAdapter
    aAdapter adapter = new aAdapter(this, generateData());

    //if extending activity
    //2. get ListView from activity_main.xml
    ListView listview = (ListView) findViewById(android.R.id.list);

    //setListAdapter
    listview.setAdapter(adapter);
    setListAdapter(adapter);


}

private ArrayList<Model> generateData(){
    ArrayList<Model> models = new ArrayList<Model>();
    models.add(new Model("Semua Tentang Zakat"));
    models.add(new Model("Apa itu Zakat?", "01"));
    models.add(new Model("Mustahik dan Muzaki", "02"));
    models.add(new Model("Dalil Mengenai Zakat", "03"));
    models.add(new Model("Keutamaan Menunaikan Zakat", "04"));

    models.add(new Model("Macam Macam Zakat"));
    models.add(new Model("Zakat Fitrah", "01"));
    models.add(new Model("Zakat Emas dan Perak", "02"));
    models.add(new Model("Zakat Ternak : Unta", "03"));
    models.add(new Model("Zakat Ternak : Sapi", "04"));
    models.add(new Model("Zakat Ternak : Kambing", "05"));
    models.add(new Model("Zakat Hasil Bumi", "06"));

    models.add(new Model("Kalkulator Zakat"));
    models.add(new Model("Zakat Fitrah", "01"));
    models.add(new Model("Zakat Emas dan Perak", "02"));
    models.add(new Model("Zakat Ternak : Unta", "03"));
    models.add(new Model("Zakat Ternak : Sapi", "04"));
    models.add(new Model("Zakat Ternak : Kambing", "05"));
    models.add(new Model("Zakat Hasil Bumi", "06"));
    return models;
}

@Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //get selected item
    String selectedValue = (String) getListAdapter().getItem(position);
    Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

当我尝试单击列表项时,我可以读取的错误是 Model cannot be cast to java.lang.string at MainActivity on ListItemClick ..

请有人帮助我,我是这里的新手.. 谢谢..对不起我的英语不好

4

2 回答 2

0

ArrayList<Model> models在全球范围内声明。

然后在里面onListItemClick做:

String selectedValue = (String) models.get(position).getTitle();
于 2013-11-11T04:11:54.997 回答
0

getItem()方法返回一个Model对象,所以如果你想要这个title字段,你必须调用相应的getter:

String selectedValue = (String) getListAdapter().getItem(position).getTitle();
于 2013-11-11T03:37:12.977 回答