Android 按id从列表行内的文本视图中读取文本

Android 按id从列表行内的文本视图中读取文本,android,android-listview,Android,Android Listview,我有一个由我的数据库填充的列表。每个列表行都有一个文本视图和两个按钮。我想循环浏览所有列表行,检查列表中每个文本视图的文本,并根据读入的文本更改其中一个按钮的背景 这是我的密码: Button fav, trash; ListView lv; TextView tv; Cursor data; CursorAdapter dataSource; @Override public void onCreate(Bundle savedInstanceState) {

我有一个由我的数据库填充的列表。每个列表行都有一个文本视图和两个按钮。我想循环浏览所有列表行,检查列表中每个文本视图的文本,并根据读入的文本更改其中一个按钮的背景

这是我的密码:

Button fav, trash;
ListView lv;
TextView tv;
Cursor data;
CursorAdapter dataSource;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hs);


        tv = (TextView) this.findViewById(R.id.first);
        trash = (Button) this.findViewById(R.id.trashButton);
        fav = (Button) this.findViewById(R.id.favButton);



        data.moveToNext();

        dataSource = new SimpleCursorAdapter(this, R.layout.phrasebook, data,
                fields, new int[] { R.id.first}, 0);

        lv = (ListView) findViewById(R.id.list_view);

        lv.setAdapter(dataSource);
您可以看到列表由数据源填充,文本视图“first”由字符串填充。我想在每个列表行中循环阅读文本视图,然后根据文本内容更改按钮背景。我的问题是如何获得文本视图和按钮的id

我仔细阅读并发现了get child and parent,但不确定这是否正确。我想如果我可以通过id访问列表行,然后访问它的子行,那么我可以这样做,但我不知道这是否可行。任何建议都将不胜感激

我已经尝试过创建一个自定义适配器,我已经让它工作了,还找到了一种访问位置的方法,我希望利用它来访问文本视图

公共类AdapterEx扩展了SimpleCursorAdapter {

以下是一个例子:

public class AdapterEx extends ArrayAdapter<String>
{

//You are going to pass a array of String, that you'll use at the textview.

Context context;
int resourceId;
ArrayList<String> tempStrings;

public AdapterEx ( Context context, int resourceId, ArrayList<String> tempStrings) 
{

    super( context, resourceId, tempStrings);

    this.context = context;
    this.resourceId = resourceId;
    this.tempStrings= tempStrings;

}

//In this class you'll declare everything that you'll use from your xml file, like        textview, buttons and etc.
private class ViewHolder
{

    TextView textViewsHere;

    Button buttonsHere;

}

@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) 
{

    return getCustomView(position, convertView, parent);

}

@Override
public View getView( int position, View convertView, ViewGroup parent )
{

    return getCustomView(position, convertView, parent);

}

private View getCustomView( int position, View convertView, ViewGroup parent )
{

    View row = convertView;

    ViewHolder holder = null;

    if( row == null )
    {

        LayoutInflater mInflater = ( ( Activity ) context ).getLayoutInflater();
        row = mInflater.inflate( resourceId, parent, false );

        holder = new ViewHolder();

        //Now you are going to link from layout using your holder. Example:
        holder.textViewsHere = (TextView) row.findViewById( R.id.textViewsHere );

        row.setTag( holder );

    }else
    {

        holder = ( ViewHolder ) row.getTag();

    }

    //After doing the link between java code and xml.
    //You are going to set the value at the textView, buttons and whatever you want.
    //You pass to this class a arrayList<String> with the values that you want at the textview.

    String valueForTheTextView = tempStrings.get(position);

    holder.textViewsHere.setText( valueForTheTextView  );

    if( valueForTheTextView .equals("whatYouWantHere"))
    {

         button.changeTheButtonBackground(); //Example

    }

    return row;

}

}
公共类AdapterEx扩展了ArrayAdapter
{
//您将传递一个字符串数组,该数组将在textview中使用。
语境;
智力资源ID;
ArrayList临时字符串;
公共AdapterEx(上下文、int-resourceId、ArrayList临时字符串)
{
super(上下文、资源ID、临时字符串);
this.context=上下文;
this.resourceId=resourceId;
this.tempStrings=tempStrings;
}
//在这个类中,您将声明将从xml文件中使用的所有内容,如textview、按钮等。
私有类视窗持有者
{
TextView textViewsHere;
按钮在这里;
}
@凌驾
公共视图getDropDownView(int位置、视图转换视图、视图组父视图)
{
返回getCustomView(位置、转换视图、父级);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
返回getCustomView(位置、转换视图、父级);
}
私有视图getCustomView(int位置、视图转换视图、视图组父视图)
{
视图行=转换视图;
ViewHolder=null;
if(行==null)
{
LayoutInflater mInflater=((活动)上下文)。getLayoutInflater();
row=mInflater.inflate(资源ID,父项,false);
holder=新的ViewHolder();
//现在,您将使用支架从布局链接。示例:
holder.textViewsHere=(TextView)row.findViewById(R.id.textViewsHere);
row.setTag(支架);
}否则
{
holder=(ViewHolder)row.getTag();
}
//在完成java代码和xml之间的链接之后。
//您将在textView、按钮和任何您想要的位置设置值。
//您向此类传递一个arrayList,其中包含您希望在textview中显示的值。
String valueForTheTextView=tempStrings.get(位置);
holder.textViewsHere.setText(TextView的值);
if(extView.equals的值(“whatYouWantHere”))
{
button.changeTheButtonBackground();//示例
}
返回行;
}
}
用它来处理其他类似的事情 AdapterEx temp=newadapterex(activity.this,R.layout.YOUHAVETOCREATEYOURLAYOUT,theDataHere);

以下是一个示例:

public class AdapterEx extends ArrayAdapter<String>
{

//You are going to pass a array of String, that you'll use at the textview.

Context context;
int resourceId;
ArrayList<String> tempStrings;

public AdapterEx ( Context context, int resourceId, ArrayList<String> tempStrings) 
{

    super( context, resourceId, tempStrings);

    this.context = context;
    this.resourceId = resourceId;
    this.tempStrings= tempStrings;

}

//In this class you'll declare everything that you'll use from your xml file, like        textview, buttons and etc.
private class ViewHolder
{

    TextView textViewsHere;

    Button buttonsHere;

}

@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) 
{

    return getCustomView(position, convertView, parent);

}

@Override
public View getView( int position, View convertView, ViewGroup parent )
{

    return getCustomView(position, convertView, parent);

}

private View getCustomView( int position, View convertView, ViewGroup parent )
{

    View row = convertView;

    ViewHolder holder = null;

    if( row == null )
    {

        LayoutInflater mInflater = ( ( Activity ) context ).getLayoutInflater();
        row = mInflater.inflate( resourceId, parent, false );

        holder = new ViewHolder();

        //Now you are going to link from layout using your holder. Example:
        holder.textViewsHere = (TextView) row.findViewById( R.id.textViewsHere );

        row.setTag( holder );

    }else
    {

        holder = ( ViewHolder ) row.getTag();

    }

    //After doing the link between java code and xml.
    //You are going to set the value at the textView, buttons and whatever you want.
    //You pass to this class a arrayList<String> with the values that you want at the textview.

    String valueForTheTextView = tempStrings.get(position);

    holder.textViewsHere.setText( valueForTheTextView  );

    if( valueForTheTextView .equals("whatYouWantHere"))
    {

         button.changeTheButtonBackground(); //Example

    }

    return row;

}

}
公共类AdapterEx扩展了ArrayAdapter
{
//您将传递一个字符串数组,该数组将在textview中使用。
语境;
智力资源ID;
ArrayList临时字符串;
公共AdapterEx(上下文、int-resourceId、ArrayList临时字符串)
{
super(上下文、资源ID、临时字符串);
this.context=上下文;
this.resourceId=resourceId;
this.tempStrings=tempStrings;
}
//在这个类中,您将声明将从xml文件中使用的所有内容,如textview、按钮等。
私有类视窗持有者
{
TextView textViewsHere;
按钮在这里;
}
@凌驾
公共视图getDropDownView(int位置、视图转换视图、视图组父视图)
{
返回getCustomView(位置、转换视图、父级);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
返回getCustomView(位置、转换视图、父级);
}
私有视图getCustomView(int位置、视图转换视图、视图组父视图)
{
视图行=转换视图;
ViewHolder=null;
if(行==null)
{
LayoutInflater mInflater=((活动)上下文)。getLayoutInflater();
row=mInflater.inflate(资源ID,父项,false);
holder=新的ViewHolder();
//现在,您将使用支架从布局链接。示例:
holder.textViewsHere=(TextView)row.findViewById(R.id.textViewsHere);
row.setTag(支架);
}否则
{
holder=(ViewHolder)row.getTag();
}
//在完成java代码和xml之间的链接之后。
//您将在textView、按钮和任何您想要的位置设置值。
//您向此类传递一个arrayList,其中包含您希望在textview中显示的值。
String valueForTheTextView=tempStrings.get(位置);
holder.textViewsHere.setText(TextView的值);
if(extView.equals的值(“whatYouWantHere”))
{
button.changeTheButtonBackground();//示例
}
返回行;
}
}
用它来处理其他类似的事情 AdapterEx temp=newadapterex(activity.this,R.layout.YOUHAVETOCREATEYOURLAYOUT,theDataHere);

以下是一个示例:

public class AdapterEx extends ArrayAdapter<String>
{

//You are going to pass a array of String, that you'll use at the textview.

Context context;
int resourceId;
ArrayList<String> tempStrings;

public AdapterEx ( Context context, int resourceId, ArrayList<String> tempStrings) 
{

    super( context, resourceId, tempStrings);

    this.context = context;
    this.resourceId = resourceId;
    this.tempStrings= tempStrings;

}

//In this class you'll declare everything that you'll use from your xml file, like        textview, buttons and etc.
private class ViewHolder
{

    TextView textViewsHere;

    Button buttonsHere;

}

@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) 
{

    return getCustomView(position, convertView, parent);

}

@Override
public View getView( int position, View convertView, ViewGroup parent )
{

    return getCustomView(position, convertView, parent);

}

private View getCustomView( int position, View convertView, ViewGroup parent )
{

    View row = convertView;

    ViewHolder holder = null;

    if( row == null )
    {

        LayoutInflater mInflater = ( ( Activity ) context ).getLayoutInflater();
        row = mInflater.inflate( resourceId, parent, false );

        holder = new ViewHolder();

        //Now you are going to link from layout using your holder. Example:
        holder.textViewsHere = (TextView) row.findViewById( R.id.textViewsHere );

        row.setTag( holder );

    }else
    {

        holder = ( ViewHolder ) row.getTag();

    }

    //After doing the link between java code and xml.
    //You are going to set the value at the textView, buttons and whatever you want.
    //You pass to this class a arrayList<String> with the values that you want at the textview.

    String valueForTheTextView = tempStrings.get(position);

    holder.textViewsHere.setText( valueForTheTextView  );

    if( valueForTheTextView .equals("whatYouWantHere"))
    {

         button.changeTheButtonBackground(); //Example

    }

    return row;

}

}
公共类AdapterEx扩展了ArrayAdapter
{
//您将传递一个字符串数组,该数组将在textview中使用。
语境;
智力资源ID;
ArrayList临时字符串;
公共AdapterEx(上下文、int-resourceId、ArrayList临时字符串)
{
super(上下文、资源ID、临时字符串);
this.context=上下文;
this.resourceId=resourceId;
this.tempStrings=tempStrings;
}
//在这个班