如何在列表视图android中单击特定列

时间:2013-09-02 10:37:25

标签: android android-layout android-intent onclicklistener

这是我的代码:

adapter= new EditAdapter(getApplicationContext(),sectionTopic);
    setListAdapter(adapter);
    ListView li = this.getListView();
    li.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View myView, int position,
                long arg3) {

            TableLayout view = (TableLayout) myView ;
            TextView name = (TextView) view.findViewById(R.id.name);

            itemName = name.getText().toString();



            Intent intent= new Intent(EditActivity.this, SectionDetailsActivity.class);
            intent.putExtra("title", itemName);

            startActivityForResult(intent,1);

        }
    });

EditAdapter:

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

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vowView = inflater.inflate(R.layout.activity_edit, parent, false);

    if (position%2==1){
        vowView.setBackgroundColor(Color.rgb(170, 218, 203));
    }
    else{
        vowView.setBackgroundColor(Color.rgb(147, 213, 212));
    }

    TextView name = (TextView) vowView.findViewById(R.id.name);
    TextView budget = (TextView) vowView.findViewById(R.id.budget);
    TextView expense = (TextView) vowView.findViewById(R.id.expense);
    ImageView pic = (ImageView) vowView.findViewById(R.id.pic);

    budget.setText(String.format("%.2f",arrayList.get(position).getBudget()));
    name.setText(arrayList.get(position).getName());
    expense.setText(String.format("%.2f",arrayList.get(position).getMaking()));

    AsyncDownloadImage image = new AsyncDownloadImage(pic);
    image.execute(arrayList.get(position).getPic());


    return vowView;
}

我有一个tableRow的ListView,当你点击一行时,你移动到另一个屏幕。 我想要的除此之外,当我按下某个列时(在文本视图上 - “addBtn”),它将移动到另一个屏幕。

如何点击特定的?

这是我的列表视图表:

<TableRow 
    android:id="@+id/table"
    android:baselineAligned="false"
    android:background="#e0eee0" >

    <TextView
        android:id="@+id/addBtn"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:layout_margin="0.5dp"
        android:background="@drawable/plus"
        android:gravity="center"
        android:lines="2"
        android:textColor="@color/Black"

         />

    <TextView
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:id="@+id/expense"
        android:layout_margin="0.5dp"
        android:background="#e6e6fa"
        android:gravity="center"
        android:text="900"
        android:textColor="@color/Black"
        android:lines="2"
         />

    <TextView
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:id="@+id/budget"
        android:layout_margin="0.5dp"
        android:background="#ffe4e1"
        android:gravity="center"
        android:text="1000"
        android:lines="2"
        android:textColor="@color/Black" />

    <TextView
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:id="@+id/name"
        android:layout_margin="0.5dp"
        android:background="#e0eee0"
        android:gravity="center"
        android:text="מזון"
        android:textColor="@color/Black"
        android:lines="2" />
</TableRow>

2 个答案:

答案 0 :(得分:0)

要通过点击行中的某些View移动到其他活动,您需要在OnClickListener方法中将getView设置为您的视图。

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

    LayoutInflater inflater = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vowView = inflater.inflate(R.layout.activity_edit, parent, false);

    if (position%2==1){
        vowView.setBackgroundColor(Color.rgb(170, 218, 203));
    }
    else{
        vowView.setBackgroundColor(Color.rgb(147, 213, 212));
    }

    TextView name = (TextView) vowView.findViewById(R.id.name);
    TextView budget = (TextView) vowView.findViewById(R.id.budget);
    TextView expense = (TextView) vowView.findViewById(R.id.expense);
    ImageView pic = (ImageView) vowView.findViewById(R.id.pic);

    // ... here ->
    TextView button = (TextView) vowView.findViewById(R.id.addBtn);
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // open here other activity
        }
    });
    // <- ....

    budget.setText(String.format("%.2f",arrayList.get(position).getBudget()));
    name.setText(arrayList.get(position).getName());
    expense.setText(String.format("%.2f",arrayList.get(position).getMaking()));

    AsyncDownloadImage image = new AsyncDownloadImage(pic);
    image.execute(arrayList.get(position).getPic());

    return vowView;
}

答案 1 :(得分:0)

看看这个blog,这是关于listview项目中的可点击区域,这可能会有所帮助。