我有一个ListView和一个Baseadapter类,这些类将这些值附加到View。 Adapter类查看除一个变量之外的所有变量。.我不知道该变量是否超出该类的范围。.我已经检查了,但我似乎找不到问题出在哪里。

Adapter类的getView方法

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;

    if(convertView==null)
        vi = lf.inflate(R.layout.single_list_item, null);

    TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
    TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
    TextView id = (TextView) vi.findViewById(R.id.drug_id); //id
    TextView title_id = (TextView) vi.findViewById(R.id.title_i); //id
    Drug drug = data.get(position);

    // Setting all values in list view
    title.setText(drug.getName());
    description.setText(drug.get_desc());
    id.setText("drug.getName()"); // where the problem is from
    title_id.setText(drug.getName().substring(0, 1));

    Typeface tf = Typeface.createFromAsset(this.activity.getAssets(), "Roboto-Thin.ttf");
    Typeface vf = Typeface.createFromAsset(this.activity.getAssets(), "RobotoCondensed-Regular.ttf");
    title.setTypeface(tf);
    title_id.setTypeface(tf);
    description.setTypeface(vf);

    title_id.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(activity, "This is the letter button", Toast.LENGTH_SHORT).show();
            //This is working
        }
    });

    return vi;
}


然后我尝试使用List中的MainActivity似乎在那儿工作..这是方法

public void CRUDOperation(DBHandler db){
    // Inserting Contacts
    Log.d("Insert: ", "Inserting ..");
    db.addDrug(new Drug("Chloroquine", "The bitter drug that is not slimy"));
    db.addDrug(new Drug("Artesunate", "The big drug..PS: I hate it so much"));

    // Reading all contacts
    Log.d("Reading: ", "Reading all contacts..");
    List<Drug> drug = db.getAllDrugs();

    for (Drug dg : drug) {
        String log = "Id: "+dg.getID()+" , Name: " + dg.getName() + " , Desc: " + dg.get_desc();
        Log.d("Name: ", log);
    }
}


这是来自日志猫的错误

    07-10 11:48:17.045: E/AndroidRuntime(1439): FATAL EXCEPTION: main
07-10 11:48:17.045: E/AndroidRuntime(1439): android.content.res.Resources$NotFoundException: String resource ID #0x1
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.content.res.Resources.getText(Resources.java:239)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.TextView.setText(TextView.java:3844)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at com.example.utils.DrugAdapter.getView(DrugAdapter.java:64)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.ListView.onMeasure(ListView.java:1159)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.View.measure(View.java:15848)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.View.measure(View.java:15848)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.View.measure(View.java:15848)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.View.measure(View.java:15848)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.View.measure(View.java:15848)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.View.measure(View.java:15848)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.View.measure(View.java:15848)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.Choreographer.doFrame(Choreographer.java:532)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.os.Handler.handleCallback(Handler.java:730)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.os.Looper.loop(Looper.java:137)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at java.lang.reflect.Method.invoke(Method.java:525)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-10 11:48:17.045: E/AndroidRuntime(1439):     at dalvik.system.NativeStart.main(Native Method)


如果需要的话,请询问其他详细信息。.请帮助。.谢谢您的时间

编辑

single_list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_card" >

    <TextView
        android:id="@+id/title_i"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="T"
        android:textSize="40sp"
        android:textColor="@android:color/white"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:background="@color/theme_color"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="2dp" />

    <TextView
        android:id="@+id/drug_title"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="25sp"
        android:text=""
        android:layout_toRightOf="@id/title_i" />

    <TextView
        android:id="@+id/drug_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_toRightOf="@id/drug_title"
        android:textSize="13sp"
        android:layout_marginLeft="5dp" />

    <TextView
        android:id="@+id/drug_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="12sp"
        android:layout_below="@id/drug_title"
        android:layout_alignLeft="@id/drug_title"
        android:maxWidth="220dp"
        android:layout_marginTop="2dp" />

    </RelativeLayout>


Drug.java

    package com.example.utils;

public class Drug {
    int id;
    String name;
    String descri;

    public Drug(int id, String name, String desc) {
        // TODO Auto-generated constructor stub
        this.id = id;
        this.name = name;
        descri = desc;
    }

    public Drug(String name, String desc){
        this.name = name;
        descri = desc;
    }

    public Drug() {
        // TODO Auto-generated constructor stub
    }

    public int getID() {
        // TODO Auto-generated method stub
        return this.id;
    }

    public String getName() {
        // TODO Auto-generated method stub
        return this.name;
    }

    public String get_desc() {
        // TODO Auto-generated method stub
        return descri;
    }

    public void setID(int newId) {
        // TODO Auto-generated method stub
        this.id = newId;
    }

    public void setName(String newName) {
        // TODO Auto-generated method stub
        this.name = newName;
    }

    public void setDesc(String newDesc) {
        descri = newDesc;
    }

}


getAllDrugs()方法

public List<Drug> getAllDrugs(){
    List<Drug> drugList = new ArrayList<Drug>();

    String selectQuery = "SELECT  * FROM " + TABLE_DRUGS;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Drug drug = new Drug();
            drug.setID(Integer.parseInt(cursor.getString(0)));
            drug.setName(cursor.getString(1));
            drug.setDesc(cursor.getString(2));
            // Adding contact to list
            drugList.add(drug);
        } while (cursor.moveToNext());
    }

    return drugList;
}

最佳答案

您正在创建类对象Drug Drug = data.get(position);里面的getview。因此,每次创建新对象。您将获得所有数据以应用特定位置。如

声明公共变量

public List<Drug> drug=null;


方法也属于

public void CRUDOperation(DBHandler db){
    // Inserting Contacts
    Log.d("Insert: ", "Inserting ..");
    db.addDrug(new Drug("Chloroquine", "The bitter drug that is not slimy"));
    db.addDrug(new Drug("Artesunate", "The big drug..PS: I hate it so much"));

    // Reading all contacts
    Log.d("Reading: ", "Reading all contacts..");
  drug = db.getAllDrugs();

    for (Drug dg : drug) {
        String log = "Id: "+dg.getID()+" , Name: " + dg.getName() + " , Desc: " + dg.get_desc();
        Log.d("Name: ", log);
    }
}


使用getview位置

///传递arraylist参数适配器类

创建构造函数并分配

class adapter extends baseAdapter{

Arraylist<Drugs> drugs;
Context mcontext;
public adapter(Context context, Arraylist<Drugs> drugs){
this.mcontext=context;
this.drugs=drugs;
}
}



public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;

    if(convertView==null)
        vi = lf.inflate(R.layout.single_list_item, null);



    // Setting all values in list view
    title.setText(drug.get(position).getName());
    description.setText(drug.get(position).get_desc());

    title_id.setText(drug.get(position).getName().substring(0, 1));

    .....

    title_id.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(activity, "This is the letter button", Toast.LENGTH_SHORT).show();
            //This is working
        }
    });

    return vi;
}

07-24 20:34