Java 列表值对BaseAdapter类不可见

Java 列表值对BaseAdapter类不可见,java,android,listview,baseadapter,Java,Android,Listview,Baseadapter,我有一个ListView和一个Baseadapter类,它将值附加到视图。适配器类可以看到除一个之外的所有变量。我不知道该变量是否超出了类的范围。。我检查过了,似乎找不到问题出在哪里 适配器类的getView方法 public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View vi=convertView; if

我有一个
ListView
和一个
Baseadapter
类,它将值附加到
视图
。适配器类可以看到除一个之外的所有变量。我不知道该变量是否超出了类的范围。。我检查过了,似乎找不到问题出在哪里

适配器类的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;
}
然后我尝试使用
main活动中的
列表
,它似乎在那里工作。。这就是方法

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);
    }
}
如有必要,请询问更多详情。。请帮忙……谢谢你抽出时间

编辑

单个列表项.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>
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;
}
public List getalldruges(){
List drugList=new ArrayList();
String selectQuery=“SELECT*FROM”+表\u药物;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
药物=新药();
setID(Integer.parseInt(cursor.getString(0));
drug.setName(cursor.getString(1));
drug.setDesc(cursor.getString(2));
//将联系人添加到列表中
药物清单。添加(药物);
}while(cursor.moveToNext());
}
返回药单;
}

您正在创建类对象Drug Drug=data.get(position);在getview内部。所以每次创建新对象时。您将获得所有数据以应用于特定位置。比如

声明公共变量

public List<Drug> drug=null;
使用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;
}
类适配器扩展baseAdapter{
Arraylist药物;
语境;
公共适配器(上下文、Arraylist){
this.mcontext=上下文;
这就是:毒品=毒品;
}
}
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图vi=转换视图;
if(convertView==null)
vi=左充气(右布局单列表项,空);
//在列表视图中设置所有值
title.setText(drug.get(position.getName());
description.setText(drug.get(position.get_desc());
title_id.setText(drug.get(position.getName().substring(0,1));
.....
title\u id.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
Toast.makeText(活动,“这是字母按钮”,Toast.LENGTH_SHORT.show();
//这是有效的
}
});
返回vi;
}

我所需要做的就是将我在适配器类的
getView()
中创建的
Drug
对象的访问修饰符更改为final,即

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

谢谢大家抽出时间

能发布R.layout.single_list_项目吗。xml@Meghna我刚刚补充说这对我来说很好。@Meghna最近怎么样。getName和getDescription的值正在返回,但
getID()的值未返回CRUDOPATION()。我尚未检查传递的数据不是arrayList。。这只是一个列表,首先获取所有数据,然后检查它。将数据传递给适配器类。最后检查getview方法。只需printValuesId您可以使用自定义适配器轻松获取所有数据。我不明白…但返回数据的方法在
id.setText(“drug.getName()”)上方;//问题出在
getID()方法对类不可见,如果我是正确的..因为这就是logcat错误的全部原因..不要像这样编写id.setText(“+drug.getID()”);将其设为String.valueof(getId())。这没有错
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);
    }
}
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;
}
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;
}