使用列表视图中的简单适配器增加列表视图中的文本视图值单击
Posted
技术标签:
【中文标题】使用列表视图中的简单适配器增加列表视图中的文本视图值单击【英文标题】:Incremetn Textview value in listview using simpleadapter on listview click 【发布时间】:2017-03-28 04:47:38 【问题描述】:我正在处理一个片段,它包含列表视图,我想通过单击列表项行来更新数量值/
这是我的代码
public class Fruites extends Fragment
ImageButton b1,b2,b3,b4,b5;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View fruites = inflater.inflate(R.layout.fruites_frag, container, false);
//((TextView)android.findViewById(R.id.textView)).setText("Fruites");
perform(fruites);
return fruites;
ListView lv;
String[] values=new String[]"Apple", "Banana", "oranges","Strawberries", "Grapes", "Gauva";
int[] flags = new int[]R.drawable.apple,R.drawable.ban,R.drawable.oranges,R.drawable.strawb,R.drawable.images,
R.drawable.gauv ;
int inc = 0;
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
// Each row in the list stores country name, currency and flag
public void perform(View v)
// List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<5;i++)
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Name : " + values[i]);
hm.put("cur","Qty : " + inc); // i want to increment this value by clicking listview
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
// Keys used in Hashmap
String[] from = "flag","txt","cur" ;
// Ids of views in listview_layout
int[] to = R.id.flag,R.id.txt,R.id.cur;
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList, R.layout.list_row, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView )v.findViewById(R.id.listview);
//listView.bringToFront();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// here i want to implement to increment the variable by clicking the row items
Object o = parent.getItemAtPosition(position);
//String str=(String)o;
Toast.makeText(getActivity(),
"Button is clicked"+o, Toast.LENGTH_LONG).show();
);
// Setting the adapter to the listView
listView.setAdapter(adapter);
我想更新列表视图中每行显示的项目数量。
【问题讨论】:
【参考方案1】:你可以这样做:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// view variable passed is a row
TextView myCounter = (TextView)view.findViewById(R.id.my_counter);
//Assuming it already has a int value
int currentValue = Integer.parseInt(myCounter.getText().toString());
myCounter.setText(String.valueOf(currentValue + 1));
Object o = parent.getItemAtPosition(position);
//String str=(String)o;
Toast.makeText(getActivity(),
"Button is clicked"+o, Toast.LENGTH_LONG).show();
);
将my_counter
替换为您的计数器TextView's
id。
【讨论】:
以上是关于使用列表视图中的简单适配器增加列表视图中的文本视图值单击的主要内容,如果未能解决你的问题,请参考以下文章