【问题标题】:How to click on a particular column in a list view android如何单击列表视图android中的特定列
【发布时间】:2013-09-02 10:37:24
【问题描述】:

这是我的代码:

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

        }
    });

编辑适配器:

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>

【问题讨论】:

  • 一般情况下,您需要在您的适配器的getView(...) 方法内设置点击时的addBtn。我可以将您准确地引导到正确的位置,但首先更新问题并添加 EditAdapter 内容。重要的地方是public View getView(final int position, View convertView, ViewGroup parent)

标签: android android-layout android-intent onclicklistener


【解决方案1】:

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

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

【讨论】:

  • 添加代码后,没有任何效果。只有当我取消 EditActivity 中的 OnItemClickListener 时,它才有效。但我需要两个选项才能工作..
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-11
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多