如何访问arrayadapter生成的listview项

时间:2014-07-18 05:52:22

标签: android listview firebase

我已将应用程序绑定到Firebase数据库,并且在能够显示它们之后,我还希望能够选择它们并访问它们的textview值。但是.getValue()不适用于视图。我知道我的代码非常混乱,我的选择器只是单向工作,但是你可以帮我找出发生了什么。我将添加一个截图以使事情更清晰。在点击其中一个项目时,num = Integer.parseInt(text)会出现错误,应用程序将崩溃,因为我从视图中获取的值是" Android.widget ...",不是我想要的。 截图pic:

Screenshot

Orders.java

public class Orders extends ActionBarActivity {
    public final static String TOTAL_SUM = "com.nordscript.checkmate.SUM";

// Declare the UI components
    private ListView foodList;

    // Declare an ArrayAdapter that we use to join the data set and the ListView
    // is the way of type safe, means you only can pass Strings to this array
    //Anyway ArrayAdapter supports only TextView
    private ArrayAdapter arrayAdapter;

    private Firebase ref;
    private Orders activ;
    private ArrayList<String> dishes;
    public int total;

@Override
protected void onCreate(Bundle savedInstanceState) {
    activ = this;
    dishes = new ArrayList<String>(20);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_orders);

 // Initialize the UI components
    foodList = (ListView)findViewById(R.id.listView1);

    // Create a reference to a Firebase location
    ref = new Firebase("https://xxxxxxxxxx.firebaseio-demo.com/Restaurants/Restaurant 2/Tables/Table 2/Orders");

    // Read data and react to changes
    ref.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
            Map s = snapshot.getValue(Map.class);
            dishes.add(snapshot.getName() + "  " + ((Map)snapshot.getValue(Map.class)).get("Price"));
            Log.i("Test", dishes.toString());
            arrayAdapter = new ArrayAdapter(activ, android.R.layout.simple_list_item_1, dishes.toArray());

            // By using setAdapter method, you plugged the ListView with adapter
            foodList.setAdapter(arrayAdapter);
          }

          @Override public void onChildChanged(DataSnapshot snapshot, String previousChildName) { }

          @Override public void onChildRemoved(DataSnapshot snapshot) {
            Map s = snapshot.getValue(Map.class);
            dishes.remove(snapshot.getName() + "  " + ((Map)snapshot.getValue(Map.class)).get("Price"));
            Log.i("Test", dishes.toString());
            arrayAdapter = new ArrayAdapter(activ, android.R.layout.simple_list_item_1, dishes.toArray());

            // By using setAdapter method, you plugged the ListView with adapter
            foodList.setAdapter(arrayAdapter);
          }

          @Override public void onChildMoved(DataSnapshot snapshot, String previousChildName) { }

          @Override
          public void onCancelled(FirebaseError arg0) { }
    });

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

            view.setBackgroundColor(Color.GREEN);
            String text = view.toString();
            text.replaceAll("[^\\d.]", "");
            int num = Integer.parseInt(text);
            total += num;
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.orders, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void toPayment(View view) {
    Intent pay = new Intent(this, PaymentMethod.class);
    pay.putExtra(TOTAL_SUM, total);
    startActivity(pay);
}


}

5 个答案:

答案 0 :(得分:1)

我从你的问题中理解:假设你点击第3项,即“布丁8”,你想要这个值“布丁8”(textview值)。对??  如果这样你只需要在你的setOnItemClickListener内的position属性的帮助下从你的ArrayList获取你在第一个位置传递给适配器的值:

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

            view.setBackgroundColor(Color.GREEN);

            String text = dishes.get(position);
            // do what you want to do with this value.

        }
    });

答案 1 :(得分:1)

更改你的onClickListener():

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

        view.setBackgroundColor(Color.GREEN);
        String text = (String)parent.getItemAtPosition(position);
        text.replaceAll("[^\\d.]", "");
        int num = Integer.parseInt(text);
        total += num;
    }
});

或者您可以使用SimpleAdapter在单独的xml布局中使用两个textview分别显示字符串和整数值。使用它,您可以更好地获取onclick()上的值。

答案 2 :(得分:0)

您需要获取该特定视图的文字view.getText().toString();

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

            view.setBackgroundColor(Color.GREEN);
            String text = view.getText().toString();
            text.replaceAll("[^\\d.]", "");
            int num = Integer.parseInt(text);
            total += num;
        }
    });

答案 3 :(得分:0)

它可能对你有所帮助,但这是不好的方式:

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

            String item = (String) arrayAdapter.getItem(position);
            String[] split = item.split(" ");
            int num = Integer.parseInt(split[1]);
            total += num;
        }
    });

但这是非常强烈的......

至于我,你需要扩展Adapter,收集你的自定义项目,点击后获取它并获取你需要的整数值。

答案 4 :(得分:0)

我知道这可能为时已晚,但是希望 帮助正在寻找具有多个值的Custom ArrayAdapter类似解决方案的人。

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

            ListItems item = (ListItems) parent.getItemAtPosition(position);
            System.out.println("Item: "+item.getName());
        }
    });