如何使用textview创建列表视图并增加n减少按钮?

时间:2016-04-20 02:31:41

标签: android listview android-adapter

适配器类

public class MyCustomAdapter extends BaseAdapter implements ListAdapter {

    private ArrayList<String> list = new ArrayList<String>();
    private Context context;



    /*public MyCustomAdapter(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }*/
    public MyCustomAdapter(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.custom_layout, null);
    }
        TextView name = (TextView) view.findViewById(R.id.text);
        name.setText(list.get(position));
        final TextView milkCount = (TextView) view.findViewById(R.id.milkcount);
        if (milkCount.getText().toString().trim() == null) {
            milkCount.setText("0");
        }
        ImageView increment = (ImageView) view.findViewById(R.id.add);
        ImageView decrement = (ImageView) view.findViewById(R.id.sub_item);
        increment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = Integer.parseInt(milkCount.getText().toString().trim());
                a = a + 1;
                milkCount.setText("" + a);
            }
        });
        decrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = Integer.parseInt(milkCount.getText().toString().trim());
            if (a == 0) {
                a = 0;
            } else {
                a = a - 1;
            }
            milkCount.setText("" + a);
        }
    });
    return view;
}
}

主要活动

public class DailyScreen extends AppCompatActivity implements 



AdapterView.OnItemClickListener {


///Description
ImageView add, edit;
MyCustomAdapter adapter1;
Button ok;
Button next2;

final Context context = this;
ListView listView;
private ArrayAdapter<String> adapter;

public ArrayList<String> getArrayList() {
    return arrayList;
}

public void setArrayList(ArrayList<String> arrayList) {
    this.arrayList = arrayList;
}

private ArrayList<String> arrayList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.daily_listview);

    //initialization
    add = (ImageView) findViewById(R.id.add);
    edit = (ImageView) findViewById(R.id.edit);
    listView = (ListView) findViewById(R.id.list);
    next2 = (Button) findViewById(R.id.next2);

    String items[] = {"toned milk"};
    //this.setArrayList((ArrayList<String>) Arrays.asList(items));
    arrayList = new ArrayList<>(Arrays.asList(items));
    adapter1 = new MyCustomAdapter(arrayList, this);
    listView.setAdapter(adapter1);
    listView.setOnItemClickListener(this);
}
@Override
protected void onResume() {
    super.onResume();
    next2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(DailyScreen.this, SupplierActivity.class);
            startActivity(intent);
        }
    });


    add.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(DailyScreen.this);
            dialog.setTitle("Enter new Milk");
            dialog.setContentView(R.layout.dialog_box);
            dialog.setCancelable(false);// to prevent the user when he click  any where in the screen
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();

            //initialization for edit text
            final EditText editText = (EditText) dialog.findViewById(R.id.pro);
            String data = editText.getText().toString();

这里是我试过的代码我希望在列表视图中有一个文本视图n按钮,它将相对于文本视图增加n减少。当我尝试添加一个新项目时,它会在所有项目中设置0。所以我必须在从0开始之前添加所有项目。

所以请帮帮我

image

            //button initialization
            Button ok = (Button) dialog.findViewById(R.id.ok);

            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String data = editText.getText().toString();
                    if (!data.isEmpty()) {
                        arrayList.add(data);
                        adapter1=null;
                        adapter1 = new MyCustomAdapter(arrayList,DailyScreen.this);
                        listView.setAdapter(adapter1);
                        Toast.makeText(getApplicationContext(), "product name is :" + data, Toast.LENGTH_LONG).show();
                        adapter1.notifyDataSetChanged();// to   refresh with update one

                        dialog.cancel();

                    }
                    else{
                        Toast.makeText(DailyScreen.this, "Please enter the data", Toast.LENGTH_SHORT).show();
                    }


                }
            });

        }

    });



}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    View k=view;

}
// to initialize  Context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_items, menu);


}


//checking to delete the item and refreshing the items
@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.delete:
            arrayList.remove(info.position);
            //to refresh the list view
            adapter.notifyDataSetChanged();
            return true;
        default:
            return super.onContextItemSelected(item);


    }
}
}

1 个答案:

答案 0 :(得分:0)

我发现您没有映射模型对象的数量,但总是从调用notifyDataSetChanged()时生成的新文本视图中进行映射。

我也看到了一些小的编程错误,比如

  • private ArrayList<String> list = new ArrayList<String>();在构造函数中,您初始化list
  • if (milkCount.getText().toString().trim() == null)
  • adapter1=null;然后adapter1 = new ...
  • list.get(position),而您已宣布getItem(position)这样做。
  • final Context context = this;没用。

我可以继续......: - )