11

我有问题。在修改我的 XML 文件之前,我的列表视图能够完美地工作。但是现在,在对 xml 进行一些修改后,它无法正常工作。我的列表视图是自定义的。所以,我创建了单独的 xml 来呈现列表视图中的每一行。

我的单row.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/relativeLayoutSingleRowManageAddictions"
    android:layout_height="40dp"
    android:background="@drawable/box_midbg" >

    <TextView
        android:id="@+id/textViewSingleRowManageAddictions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="17dp"
        android:text="TextView"
        android:textSize="20dp"
        android:textColor="#ffffff" />

    <ImageView
        android:id="@+id/imageViewSingleRowManageAddictions"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="18dp"
        android:src="@drawable/listing_arrow" />

</RelativeLayout>

我的listview 所在的main.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_bg_edited" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        style="@style/top_bar_style" >

        <ImageView
            android:id="@+id/imageViewMAnageAddictionsBack"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/back_arrow"
            android:clickable="true" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/header_style"
            android:text="Manage Addictions" />

        <ImageView
            android:id="@+id/imageViewManageAddictionsAdd"
            android:layout_width="25dp"
            android:layout_height="20dp"
            android:layout_marginRight="3dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/plus_nav"
            android:clickable="true" />

    </RelativeLayout>

    <ListView
        android:id="@+id/listViewManageAddictions"
        android:layout_width="290dp"
        android:layout_height="fill_parent"
        android:layout_below="@+id/relativeLayout1"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="12dp"
        android:divider="@android:color/transparent"
        android:dividerHeight="2dp" />

</RelativeLayout>

还有我的java代码:

package com.addictioncounterapp;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ManageAddictionList extends Activity {
  ImageView iv_manage_addictions_back, iv_manage_addictions_add;
  ListView listview;
  ArrayList < String > arraylist_manage_addiction;
  ArrayAdapter < String > arrayadapter_manage_addiction;
  SQLiteDatabase database;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manage_addiction_list);

    loaddatabase();

    iv_manage_addictions_back = (ImageView) findViewById(R.id.imageViewMAnageAddictionsBack);
    iv_manage_addictions_back.setClickable(true);
    iv_manage_addictions_back.setOnClickListener(new OnClickListener() {@Override
      public void onClick(View v) {
        startActivity(new Intent(ManageAddictionList.this, Settings.class));
      }
    });

    iv_manage_addictions_add = (ImageView) findViewById(R.id.imageViewManageAddictionsAdd);
    iv_manage_addictions_add.setClickable(true);
    iv_manage_addictions_add.setOnClickListener(new OnClickListener() {@Override
      public void onClick(View v) {
        Intent intent = new Intent(ManageAddictionList.this, MainActivity.class);
        intent.putExtra("name", "");
        intent.putExtra("unit", "");
        intent.putExtra("attribute", "");
        intent.putExtra("limit", "");
        intent.putExtra("operation", "Add Addiction");
        startActivity(intent);
      }
    });


    arraylist_manage_addiction = new ArrayList < String > ();
    manageList();
    listview = (ListView) findViewById(R.id.listViewManageAddictions);

    if (arraylist_manage_addiction.isEmpty()) Toast.makeText(getBaseContext(), "No Addictions found to manage. Click on 'add' button to create new Addiction.", Toast.LENGTH_SHORT)
      .show();
    else listview.setAdapter(arrayadapter_manage_addiction);

    listview.setOnItemClickListener(new OnItemClickListener() {@Override
      public void onItemClick(AdapterView <? > arg0, View arg1, int arg2, long arg3) {
        String name = null, attribute = null, unit = null, limit = null;

        View parentView = (View) arg0.getParent();
        name = ((TextView) parentView.findViewById(R.id.textViewSingleRowManageAddictions))
          .getText() + "";

        Toast.makeText(getBaseContext(), name, Toast.LENGTH_SHORT)
          .show();

        int cat_id = 0;

        //--------Fetching cat_id through name from the list--------

        Cursor cursor;

        cursor = database.query("category", new String[] {
          "cat_id"
        }, new String("cat_name=?"), new String[] {
          name
        }, null, null, null);

        if (cursor.getCount() > 0) {
          while (cursor.moveToNext())
          cat_id = cursor.getInt(0);
          cursor.close();
        }

        //--------Fetching unit, attribute, limit through cat_id from the list--------

        cursor = database.query("category_attribute", new String[] {
          "cat_attribute_name", "cat_attribute_unit", "cat_limit"
        }, new String("cat_id=?"), new String[] {
          cat_id + ""
        }, null, null, null);

        if (cursor.getCount() > 0) {
          while (cursor.moveToNext()) {
            attribute = cursor.getString(0);
            unit = cursor.getString(1);
            limit = cursor.getString(2);
          }
          cursor.close();
        }

        Intent intent = new Intent(ManageAddictionList.this, MainActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("unit", unit);
        intent.putExtra("attribute", attribute);
        intent.putExtra("limit", limit);
        intent.putExtra("cat_id", cat_id);
        intent.putExtra("operation", "Edit Addiction");
        startActivity(intent);
      }
    });
  }

  private void loaddatabase() {
    database = openOrCreateDatabase("AddictionCounter.db", SQLiteDatabase.OPEN_READONLY, null);
  }

  private void manageList() {
    String[] columns = {
      "cat_name"
    };
    Cursor cursor;

    cursor = database.query("category", columns, null, null, null, null, null);

    if (cursor.getCount() > 0) {
      while (cursor.moveToNext())
      arraylist_manage_addiction.add(cursor.getString(0));
      cursor.close();
    }

    arrayadapter_manage_addiction = new ArrayAdapter < String > (this, R.layout.single_row_manage_addictions, R.id.textViewSingleRowManageAddictions, arraylist_manage_addiction);
  }
}

这背后的主要错误是,当我使用这个获得名称时:

View parentView = (View) arg0.getParent();
name = ((TextView) parentView.findViewById(R.id.textViewSingleRowManageAddictions)).getText()+"";

任何记录的列表视图,它只给出第一条记录的名称。例如,如果我的第一行有名为“Gaming”的文本视图,当我单击任何行时,(出于调试目的,我使用Toast.makeText(...))Toasts “Gaming”作为列表视图中每条记录的名称,尽管列表视图中的每条记录都是独一无二。请帮我解决这个问题。

4

5 回答 5

54
lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // selected item
        String selected = ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString();

        Toast toast = Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
        toast.show();
    }
});
于 2013-02-15T09:04:59.780 回答
4
listView = (ListView)view.findViewById(R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String selected = ((TextView) view.findViewById(R.id.complaint)).getText().toString();
        Toast.makeText(context,selected,Toast.LENGTH_LONG).show();
    }
});
于 2016-06-09T10:24:05.937 回答
2

如果您只想要onClickListenerTextView最好onClickListener在适配器本身中定义。像

yourTextView.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        // showToast()
      }
});
于 2013-02-15T09:00:42.110 回答
1

除了上述解决方案之外,这对我有用,我遇到了类似的情况,在添加以下内容后我能够解决它

android:descendantFocusability="blocksDescendants"

到我的 xml 列表视图。它现在正在工作。对于真正好奇的用户,这里有更多关于 http://developer.android.com/reference/android/R.attr.html#descendantFocusability的信息

于 2014-12-24T04:30:51.187 回答
0

因为我没有足够的声誉来发表评论在这里我正在评论您对SKK答案的问题只需将if语句添加到您并执行不同的操作...

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    // selected item
    String selected = ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString();

    if(selected.equals("Your comparing string, like Games"){                                               

    Toast toast = Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
    toast.show(); }                                                                                                                 

    else {                                            
    if(selected.equals("Your comparing string, like Games")){                                            
    Toast toast = Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
    toast.show();}}


}});                                                   
于 2017-11-30T08:23:01.297 回答