user5083862
user5083862

Reputation: 67

Retrieved values when listview item is clicked

I created a listview using an adapter. the listview contains the distinct values of a particular field in a table. now,when the listview item is clicked, it should retrieve all items which have the id of that particular data. i manage to get the data from the listview but when the items are not fetched properly.

row.xml

    <ImageView
    android:id="@+id/img"
    android:src="@mipmap/ic_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <TextView
    android:id="@+id/txtid"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="sd"
    />

Tab2.java

 ListView list;
    final ArrayList<HashMap<String, String>> mylist;
    ListAdapter adapter;
    HashMap<String, String> map2;
   final TextView a1;
    list = (ListView) v.findViewById(R.id.listView2);
    a1 = (TextView) v.findViewById(R.id.txt1);
    mylist = new ArrayList<HashMap<String, String>>();

    DatabaseHandler db = new DatabaseHandler(getActivity());
    final List<Product> sent = db.getSentProducts();
    for (Product s : sent) {
        map2 = new HashMap<String, String>();
        map2.put("txtid", s.getMsg_id());
        mylist.add(map2);
        Log.d("msgid", s.getMsg_id());
    }
    for (Product s : sent) {
    }
    try {
        adapter = new SimpleAdapter(getActivity(),mylist,R.layout.sent_row, new String[]{"txtid"}, new int[]{R.id.txtid});
        list.setAdapter(adapter);
    } catch (Exception e) {
    }


    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            TextView a = (TextView)v.findViewById(R.id.txtid);
            String content1 = a.getText().toString();
            DatabaseHandler db = new DatabaseHandler(getActivity());
            List<Product> sentinfo =  db.getSentProductInfo(content1);

           for (Product si : sentinfo){

                   Log.d("asdsd", si.getStorCode()+" "+si.getCode()+" "+si.getBstock()+" "+si.getDeliveries()+" "+si.getSpoilage()+" "+ si.getEstock()+" ");
          }

            return true;
        }
    });
    return v;
}

db

enter image description here

android ui

enter image description here

When i click 20150824070159 in the listview, it should fetch two rows in the table, however, it only fetches one and it is the data of the other id, 20150820162104.

How will i be able to answer this?

Upvotes: 0

Views: 539

Answers (1)

George Rappel
George Rappel

Reputation: 7198

To get the text from the item clicked on the list view you should do this:

 list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

           String clickedText = list.getItemAtPosition(position).toString();                

        }
    });

Just make sure that your list variable is declared on the global scope, outside of any method, because this function will be called after the onCreate or any other method has already ended.

Upvotes: 1

Related Questions