0

我是 Android 新手,正在使用自定义列表视图,它运行良好,但问题是当我单击单个列表以显示列表详细信息时,每次它只显示最后一个列表详细信息。在 onItemClick 方法中,只有最后一个发票 id 被传递...这里是代码

 try {

      //*** Getting Array of Attributes
            attributes = jsonreturn.getJSONObject(TAG_ATTRIBUTE);
            String status = attributes.getString(TAG_VALIDCODE);
            JSONObject invoices = jsonreturn.getJSONObject(TAG_INVOICELIST);
            JSONArray invoice = invoices.getJSONArray(TAG_INVOICE);

            if(status.equals("200")){
     // looping through All Invoice
       for(int i = 0; i < invoice.length(); i++)
         {


            JSONObject c = invoice.getJSONObject(i);

     //***** Storing each JSON item in variable
            JSONObject client = c.getJSONObject(TAG_CLIENT);
            String organization = client.getString(TAG_ORGANIZATION);
            String invoiceno = c.getString(TAG_ID);
            String date = c.getString(TAG_DATE);
            String currency = c.getString(TAG_CURRENCY);
            String outstanding = c.getString(TAG_OUTSTANDING);
            String invoice_status = c.getString(TAG_STATUS);
            invoice_id = c.getString(TAG_INVOICE_ID);



    //**** creating new HashMap

            HashMap<String, String> map = new HashMap<String, String>();

   //*** adding each child node to HashMap key => value
            map.put(TAG_ORGANIZATION, organization);
            map.put(TAG_ID, invoiceno);
            map.put(TAG_DATE, date);
            map.put(TAG_CURRENCY, currency);
            map.put(TAG_OUTSTANDING, outstanding);
            map.put(TAG_STATUS, invoice_status);
            map.put(TAG_INVOICE_ID, invoice_id);

  //**** adding HashList to ArrayList

            invoiceList.add(map);

            }
       }

       else{
            Toast.makeText(this, "Invalid Details", 1000).show();       }


    }
           catch (JSONException e)
           {
           e.printStackTrace();
           }

     /**
     Updating parsed JSON data into ListView
     * */

    InvoiceListAdapter invoiceadapter = new InvoiceListAdapter(this, invoiceList);

    mListView.setAdapter(invoiceadapter);

 //****** Launching new screen on Selecting Single ListItem

 mListView.setOnItemClickListener(new OnItemClickListener() {


        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            // getting values from selected ListItem

            //--String date = ((TextView) view.findViewById(R.id.date)).getText().toString();
            //--String status = ((TextView) view.findViewById(R.id.invoicestatus)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), PreviewInvoice.class);
            in.putExtra("myinvoiceid", invoice_id);
            in.putExtra("uniquetoken", tokenid);
           //-- in.putExtra(TAG_DATE, date);
            //---in.putExtra(TAG_STATUS, status);
            startActivity(in);
        }
    }); 

我的 InvoiceList 适配器类代码

public class InvoiceListAdapter extends BaseAdapter {

//--private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;

public InvoiceListAdapter(ManageInvoice manageInvoice, ArrayList<HashMap<String, String>> invoiceList) {
   //-- activity = manageInvoice;
    data=invoiceList;

    inflater = (LayoutInflater)manageInvoice.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    if(null==data){
        return 0;
    }
    return data.size();
}

public Object getItem(int position) {
    if(null==data || data.size()<=position){
        return null;
    }
    return data.get(position);
}

public long getItemId(int position) {
    return position;
}

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

    View vi=convertView;
    if(convertView==null)

    vi = inflater.inflate(R.layout.invoice_list, null );

    TextView title = (TextView)vi.findViewById(R.id.organization);
    TextView invoiceId = (TextView)vi.findViewById(R.id.invoice_id);
    TextView date = (TextView)vi.findViewById(R.id.date);
    TextView currency = (TextView)vi.findViewById(R.id.currency);
    TextView currencyvalue = (TextView)vi.findViewById(R.id.outstanding);
    TextView status = (TextView)vi.findViewById(R.id.invoice_status);
   // ImageView thumb_image=(ImageView)vi.findViewById(R.id.invoice_status);

    HashMap<String, String> Invoice = new HashMap<String, String>();
    Invoice = data.get(position);

    // Setting all values in listview
    title.setText(Invoice.get(ManageInvoice.TAG_ORGANIZATION));
    invoiceId.setText(Invoice.get(ManageInvoice.TAG_ID));
    date.setText(Invoice.get(ManageInvoice.TAG_DATE));
    currency.setText(Invoice.get(ManageInvoice.TAG_CURRENCY));
    currencyvalue.setText(Invoice.get(ManageInvoice.TAG_OUTSTANDING));
    status.setText(Invoice.get(ManageInvoice.TAG_STATUS));


    return vi;
}

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

}

4

2 回答 2

1
 public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
              // Starting new intent
            Intent in = new Intent(getApplicationContext(), PreviewInvoice.class);
            in.putExtra("myinvoiceid",invoiceList.get(position).get(TAG_INVOICE_ID));
            in.putExtra("uniquetoken", tokenid); // Actually I can't find your tokenId in HashMAp object
            startActivity(in);
        }
    }); 

因为您在 中引用 invoice_id 的静态值,所以onItemClick()只需从适配器获取定位HashMap对象并使用它的键获取invoice_id.

invoiceadapter.get(position)这为您提供了一个用于单击位置的对象ListView。并使用 TAG_INVOICE_ID来自 HashMap 对象的键,您可以获得它的值。

于 2012-05-08T05:40:57.233 回答
0

这是因为您的 JSON 对象已经迭代到 invoice.length() 并且您传递到 Intent 的 invoice_id 和 tokenid 内容是最后一条记录的记录或值。将您的 JSON 对象移动到所需位置 onitemclick 并从那里检索变量的值。

于 2012-05-08T05:38:41.640 回答