如何在单个列表视图中添加两个适配器

时间:2016-03-17 02:25:06

标签: android listview

  

我尝试在单个列表中显示两个textview。但我不能一次显示两个适配器。我尝试在运行时更改textview的值。它从对话框中获取更新值。它工作但不能一次显示两个适配器。我的代码是

public class MainActivity extends ListActivity {
ArrayList<String> arrayList,a1;
ArrayAdapter<String> arrayAdapter,a2;
RatingBar r1,r2,r3,r4,r5;
TextView t;
Float beh,b,f;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView=(ListView) findViewById(R.id.list);
    String[] items={"Apple","Banana","Coconut","Grape","Peach","Pear"};
    String[] i={"0","0","0","0","0","0"};
    a1=new ArrayList<>(Arrays.asList(i));
    arrayList=new ArrayList<>(Arrays.asList(items));
    arrayAdapter=new ArrayAdapter<String>(this, R.layout.layout, R.id.textView,arrayList);
    a2=new ArrayAdapter<String>(this, R.layout.layout, R.id.textView2,a1);
    listView.setAdapter(a2);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Show input box
            showInputBox(a1.get(position),position);
        }
     });

     }
     public void showInputBox(String oldItem, final int index){
     final Dialog dialog=new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.layout_dialog);
    dialog.setTitle("School details");
    r1 = (RatingBar)dialog.findViewById(R.id.ratingBar);
    r2 = (RatingBar)dialog.findViewById(R.id.ratingBar2);

    Button s=(Button)dialog.findViewById(R.id.button);
    s.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            f = (beh + b) / 2;
            a1.set(index, f.toString());
            a2.notifyDataSetChanged();
            dialog.dismiss();
        }
    });
    dialog.show();

    r1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            beh = rating;
            //String beh = Float.toString(rating);

            Toast.makeText(getApplicationContext(), beh.toString(), Toast.LENGTH_SHORT).show();

        }
       });
       r2.setOnRatingBarChangeListener(new     RatingBar.OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

             b = rating;
            //String b = Float.toString(rating);

            Toast.makeText(getApplicationContext(), b.toString(), Toast.LENGTH_SHORT).show();

        }
     });
   }

}

2 个答案:

答案 0 :(得分:3)

简单的答案是......你不能

你要么需要两个单独的列表。 或者在适配器内部,操作两个单独的列表以产生您想要的结果。

在不了解您想要实现的目标的情况下,无法提供更多帮助。

编辑: 要为每行添加更多文本视图等,您需要一个自定义适配器。 这是一个很好的教程 - http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown

答案 1 :(得分:1)

  

通过使用自定义适配器我可以使用两个textView在运行时在listView中处理。我用这段代码解决了这个问题

       private AdapterView.OnItemClickListener onItemClickListener() {
        return new AdapterView.OnItemClickListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Dialog dialog = new Dialog(student_login.this);
            dialog.setContentView(R.layout.layout_dialog);
            int layout=R.layout.item_listview;



            final School staf = (School) parent.getAdapter().getItem(position);
            final String stname = staf.getName().toString();
            String scname = staf.getSchool();
            final String poid=staf.getObjid();
            final String pb = staf.getBehaviour();
            final String pk = staf.getKnowledge();
            final String pp= staf.getPresentation();
            final String pe=staf.getExplanation();
            final String ppu = staf.getPunctuality();
            final String c = staf.getCoun();

            Toast.makeText(getApplicationContext(), poid, Toast.LENGTH_SHORT).show();

            dialog.setTitle(scname + "-" + stname);

            dialog.show();

            r1 = (RatingBar)dialog.findViewById(R.id.ratingBar);
            r2 = (RatingBar) dialog.findViewById(R.id.ratingBar2);
            r3 = (RatingBar) dialog.findViewById(R.id.ratingBar3);
            r4 = (RatingBar) dialog.findViewById(R.id.ratingBar4);
            r5 = (RatingBar) dialog.findViewById(R.id.ratingBar5);
            Button sav= (Button)dialog.findViewById(R.id.button);

            sav.setTag(position);



            r1.setOnRatingBarChangeListener(new              RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

                    beh = rating;

                    // Toast.makeText(getApplicationContext(), beh, Toast.LENGTH_SHORT).show();

                }
            });
            r2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

                    k=rating;

                    // Toast.makeText(getApplicationContext(), k, Toast.LENGTH_SHORT).show();

                }
            });


            sav .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String position =v.getTag().toString();
                    int po=Integer.parseInt(position);
                    f=(beh+k+p+e+pu)/5;
                    String sf=String.valueOf(f);

                    View view = listView.getChildAt(po);
                    TextView someText = ((TextView) view.findViewById(R.id.textView6));
                    someText.setText(f.toString());

                    String tsf=staf.getCrate();
                    Toast.makeText(getApplicationContext(), tsf, Toast.LENGTH_SHORT).show();



                    dialog.dismiss();

                }
            });


        }
    };
}