无法在LinearLayout中获取TextView的值

时间:2015-04-05 20:49:15

标签: java android

ListView我有LinearLayout s(LL)列表。每个LinearLayout(LL)有2 LinearLayout s(LL1和LL2)。第二个LinearLayout(LL2)有TextView我希望TextView通过LinearLayout获取OnButtonCliclListener的价值

以下是我的适配器的代码:

int resource;
TextView tt;
LinearLayout LL;
LinearLayout LL2;
LinearLayout LL1;
TextView currentText;
public Vk_row_adapter(Context _context, List<? extends Map<String, ?>> data, int _resource, String[] from, int[] to) {
    super(_context, data, _resource, from, to);
    this.results = data;
    context = _context;
    resource = _resource;


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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(resource, parent, false);
    LL= (LinearLayout)v.getRootView();
    LL1 = (LinearLayout)LL.getChildAt(0);
    LL1.setOnClickListener(onButtonClickListener);
    LL2 = (LinearLayout)LL.getChildAt(1);
    LL2.setOnClickListener(onButtonClickListener);
    currentText = (TextView)LL2.getChildAt(0);

    tt = (TextView) v.findViewById(R.id.vk_name);
    tt.setText((CharSequence) results.get(position).get(ATTRIBUTE_NAME_TEXT_NAME));

    //other elements

    return v;
}
private View.OnClickListener onButtonClickListener = new View.OnClickListener()
{
     Toast.makeText(context, currentText.getText(), Toast.LENGTH_LONG).show();
};

它只提供了TextView的最后一个文本。我知道为什么。但我不知道如何正确地做到这一点。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

...真

你有一个变量存储LL2的当前文本而不存储它用于LL1,但你使用相同的onclicklistener ...

这里有几个解决方案,为了简单起见,我们先使用make第二个变量和onclicklistener

答案 1 :(得分:0)

这是我的溶剂

public class Vk_row_adapter extends SimpleAdapter {
final String ATTRIBUTE_NAME_TEXT_NAME = "text_name";
final String ATTRIBUTE_NAME_TEXT_RAITING = "text_place";
final String ATTRIBUTE_NAME_IMAGE = "image";
private List<? extends Map<String, ?>> results;
Context context;
String Name=null;
int resource;
TextView tt;
LinearLayout LL;
LinearLayout LL2;
LinearLayout LL1;
TextView currentText;
public Vk_row_adapter(Context _context, List<? extends Map<String, ?>> data, int _resource, String[] from, int[] to) {
    super(_context, data, _resource, from, to);
    this.results = data;
    context = _context;
    resource = _resource;


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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(resource, parent, false);
    LL= (LinearLayout)v.getRootView();
    LL.setOnClickListener(onButtonClickListener);
    tt = (TextView) v.findViewById(R.id.vk_name);
    tt.setText((CharSequence) results.get(position).get(ATTRIBUTE_NAME_TEXT_NAME));

    TextView bt = (TextView) v.findViewById(R.id.vk_raiting);
    bt.setText((CharSequence) results.get(position).get(ATTRIBUTE_NAME_TEXT_RAITING));

    ImageView vt = (ImageView)v.findViewById(R.id.vk_photo);
    vt.setImageBitmap((android.graphics.Bitmap) results.get(position).get(ATTRIBUTE_NAME_IMAGE));

    return v;
}
private View.OnClickListener onButtonClickListener = new View.OnClickListener()
{
    @Override
    public void onClick(View view) {
        LinearLayout asd = (LinearLayout)view;
        LL2 = (LinearLayout)asd.getChildAt(1);
        currentText = (TextView)LL2.getChildAt(0);
        new AlertDialog.Builder(context)
                .setTitle(currentText.getText())
                .setMessage("Перейти на страницу Вконтакте?")
                .setNegativeButton(android.R.string.no, null)
                .setPositiveButton("Да",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {

                            }
                        }).create().show();
    }
};

private String getName()
{
    int i = Top_Tab2.VkRowListView2.indexOfChild(tt);
    return String.valueOf(i);
}

}