Вопрос

I' trying to develop an Android app and I'm currently stuck with something.

I use a custom listview that looks as follow:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp" >

    <TextView
        android:id="@+id/name"
        style="@style/..."/>

    <TextView
        android:id="@+id/number"
        android:layout_below="@+id/..."
        style="@style/..." />

    <TextView
        android:id="@+id/id" 
        android:layout_below="@+id/..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible" />


</RelativeLayout>

The ID's here gets their text from my SQLite-database, where I query through my Contacts-table.

Now I want something to happen when I click on an item that is being presented in the listview. The click is registered but for now I want a Toast that just shows the number that is being passed into ID.(Primary keys for each row).

I can't have the arrayindex +1 since I don't order my query by id.

Here is my onItemClick:

contactList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            Toast.makeText(Contacts.this,"My unique rowID should be presented here.",
                      Toast.LENGTH_SHORT).show();

        }

    });

I'm sending the unique ID's to the textview since I believe it will make it easier to extract that info when clicking the specific item.

My base Adapter:

public class ListViewCustomAdapter extends BaseAdapter {

    public ArrayList<String> name;
    public ArrayList<String> number;
    public ArrayList<String> id;

    public Activity context;

    public LayoutInflater inflater;

    public ListViewCustomAdapter(Activity context,
            ArrayList<String> name, ArrayList<String> number, ArrayList<String> id) {
        // TODO Auto-generated constructor stub
        super();
        this.context = context;
        this.name = name;
        this.number = number;
        this.id = id;

        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return name.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int id) {

        return id;
    }

    public class ViewHolder {
        TextView txtViewName;
        TextView txtViewNumber;
        TextView txtViewId;

    }


    public View getView(int position, View convertview, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder;
        if (convertview == null) {
            holder = new ViewHolder();
            convertview = inflater.inflate(R.layout.listview_row, null);

            holder.txtViewName = (TextView) convertview
                    .findViewById(R.id.name);
            holder.txtViewNumber = (TextView) convertview
                    .findViewById(R.id.number);
            holder.txtViewId = (TextView) convertview
                    .findViewById(R.id.id);

            convertview.setTag(holder);
        } else {
            holder = (ViewHolder) convertview.getTag();
        }

        holder.txtViewName.setText(name.get(position));
        holder.txtViewNumber.setText(number.get(position));
        holder.txtViewId.setText(id.get(position));

        return convertview;
    }
}

Any help would be appreciated.

Это было полезно?

Решение

For now I want a Toast that just shows the number that is being passed into ID. (Primary keys for each row).

If you want the primary key from your database, (depending on the adapter) it should be the id parameter passed to onItemClick():

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // Use id
    Toast.makeText(Contacts.this, id + "",Toast.LENGTH_SHORT).show();

    // Or you can find it yourself
    TextView idTV = (TextView) view.findViewById(R.id.id);
    Toast.makeText(Contacts.this, idTV.getText().toString(),Toast.LENGTH_SHORT).show();
}

Addition
Now that you posted your adapter I see that you can adjust your getItemId() method to return data from your ArrayList<String> id to make the id parameter in onItemClick() work:

public long getItemId(int position) {
    return Long.parseLong(id.get(position));
}

(You can even change the container type from ArrayList<String> to ArrayList<Long> if it makes sense in your context.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top