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

}

リスト項目をクリックしようとすると、ListItemClick の MainActivity でモデルを java.lang.string にキャストできないというエラーが表示されます。

誰か助けてください私はここの初心者です..ありがとう..私の下手な英語でごめんなさい

4

2 に答える 2

0

getItem()Modelメソッドはオブジェクトを返すため、titleフィールドが必要な場合は、対応するゲッターを呼び出す必要があります。

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

ArrayList<Model> modelsグローバルに宣言します。

次に、内部で次のことをonListItemClick行います。

String selectedValue = (String) models.get(position).getTitle();
于 2013-11-11T04:11:54.997 に答える