无法将Model类上的String转换为Activity_Main

时间:2013-11-11 03:08:45

标签: java android

也许你对我的头衔感到困惑。所以我将在这里解释我的问题。 我使用自定义布局,适配器创建listview活动。但是当我尝试设置clickItemListListener它的力量关闭了。请帮帮我这是代码

Model.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;
}

}

当我尝试单击列表项时,我能读到的错误是模型无法在ListItemClick上的MainActivity中转换为java.lang.string ..

请有人帮助我,我在这里是新手..谢谢......我可怜的英语

2 个答案:

答案 0 :(得分:0)

getItem()方法会返回Model个对象,因此如果您需要title字段,则必须调用相应的getter:

String selectedValue = (String) getListAdapter().getItem(position).getTitle();

答案 1 :(得分:0)

全球宣布ArrayList<Model> models

然后在onListItemClick内执行:

String selectedValue = (String) models.get(position).getTitle();