简体   繁体   中英

List Value not visible to BaseAdapter Class

I have a ListView and a Baseadapter class that attaches the Values to the View . The Adapter class see all the variable except one..i don't know if the variable is beyond the scope of the class or not.. i have checked and i can't seem to find where the problem is from

getView method of the Adapter class

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

And then i tried using the List from the MainActivity it seems to be working there.. this is the method

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

and this is the error from the log cat

    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)

Please ask for any further details if need be.. pls help..thank you for your time

EDIT

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() method

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

You are creating class object Drug drug = data.get(position); inside getview. So every time create new object. You are get all data's to apply specific position. such as

Declare public variables

public List<Drug> drug=null;

methods belongs too

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

use getview position

/// pass arraylist parameter adapter class

create constructor and assign

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

All i needed to do was to change the access modifier of the Drug object i created in the getView() of the Adapter class to final ie

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); //title_id
    View crcl = vi.findViewById(R.id.testo);
    final Drug drug = data.get(position); //this line

    // Setting all values in list view
    title.setText(drug.getName());
    description.setText(drug.get_desc());
    id.setText(""+drug.getID());
    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
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);

            builder.setTitle("Choose a Color");
            builder.setView(lf.inflate(R.layout.color_dialog, null));
            builder.show();
            //Toast.makeText(activity, "test" + drug.getID(), Toast.LENGTH_SHORT).show();
        }
    });

    return vi;
}

Thank you for your time y'all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM