gpt4 book ai didi

java - 可选择的自定义ListView

转载 作者:行者123 更新时间:2023-12-02 12:17:14 25 4
gpt4 key购买 nike

我发现了几个类似的问题,并且我确实知道我应该将 setOnItemClickListener 用作:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(position) {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String Value = listView.getItemAtPosition();
}
});

但是,我无法使其正常工作。这有点棘手,因为我是从 JSON 数组动态生成 listView。

我正在使用 Intent 将数据作为字符串传递给 DisplayListView 类:

public class DisplayListView extends AppCompatActivity {
String response;
JSONObject jsonObject;
JSONArray jsonArray;
DrinksAdapter drinksAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_listview_layout);
listView = (ListView) findViewById(R.id.listview);
drinksAdapter = new DrinksAdapter(this,R.layout.row_layout);
listView.setAdapter(drinksAdapter);
response = getIntent().getExtras().getString("json_data");

try {
jsonObject = new JSONObject(response);
jsonArray = new JSONObject(response).getJSONArray("Menu");
//jsonArray = jsonObject.getJSONArray("Menu");
int count = 0;
String ime,kolicina,cijena;


while(count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
ime = JO.getString("ime");
kolicina = JO.getString("kolicina");
cijena = JO.getString("cijena");

Drinks drinks = new Drinks(ime,kolicina,cijena);
drinksAdapter.add(drinks);

count++;

}


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

}
}

在上面,我循环遍历数组并通过调用 DrinksAdapter 初始化数据:

public class DrinksAdapter extends ArrayAdapter {
List list = new ArrayList();

public DrinksAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
}


public void add(Drinks object) {
super.add(object);
list.add(object);
}

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

@Nullable
@Override
public Object getItem(int position) {

return list.get(position);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View row;
row = convertView;
DrinkHolder drinkHolder;

if(row == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
drinkHolder = new DrinkHolder();
drinkHolder.tx_ime = (TextView) row.findViewById(R.id.tx_ime);
drinkHolder.tx_kolicina = (TextView) row.findViewById(R.id.tx_kolicina);
drinkHolder.tx_cijena = (TextView) row.findViewById(R.id.tx_cijena);

row.setTag(drinkHolder);
} else {
drinkHolder = (DrinkHolder) row.getTag();
}


Drinks drinks = (Drinks) this.getItem(position);
drinkHolder.tx_ime.setText(drinks.getIme());
drinkHolder.tx_kolicina.setText(drinks.getKolicina());
drinkHolder.tx_cijena.setText(drinks.getCijena());
return row;

}

static class DrinkHolder {
TextView tx_ime,tx_kolicina,tx_cijena;
}

}

Drinks 类用于初始化变量。

哪里可以实现这个功能?

我知道我有很多东西要学,而且我每天都在这样做,看起来我现在有点卡住了:(

我们非常感谢任何形式的帮助。谢谢!

最佳答案

您可以通过 getView 方法激活 View 中某些元素的监听器

drinkHolder.tx_cijena .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//using value of `int position` in `getView` to know which item in the list is clicked
}


像这样

public class DrinksAdapter extends ArrayAdapter {
List list = new ArrayList();

public DrinksAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
}


public void add(Drinks object) {
super.add(object);
list.add(object);
}

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

@Nullable
@Override
public Object getItem(int position) {

return list.get(position);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View row;
row = convertView;
DrinkHolder drinkHolder;

if(row == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
drinkHolder = new DrinkHolder();
drinkHolder.tx_ime = (TextView) row.findViewById(R.id.tx_ime);
drinkHolder.tx_kolicina = (TextView) row.findViewById(R.id.tx_kolicina);
drinkHolder.tx_cijena = (TextView) row.findViewById(R.id.tx_cijena);

row.setTag(drinkHolder);
} else {
drinkHolder = (DrinkHolder) row.getTag();
}


final Drinks drinks = (Drinks) this.getItem(position); //make it final
drinkHolder.tx_ime.setText(drinks.getIme());
drinkHolder.tx_kolicina.setText(drinks.getKolicina());
drinkHolder.tx_cijena.setText(drinks.getCijena());
drinkHolder.tx_ime.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
//do anything with your **drink** object get any present value
Log.i("INFO", drinks.getIme()); // you will log whichever text is clicked in Ime of each list item
}
}
return row;

}

static class DrinkHolder {
TextView tx_ime,tx_kolicina,tx_cijena;
}

}

关于java - 可选择的自定义ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46084092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com