我发现了几个这样的问题,我确实认为我应该使用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
类用于初始化变量。
我在哪里可以实现此功能?
我知道我有很多东西需要学习,我每天都这样做,看起来我现在只是有点卡住了:(
非常感谢任何形式的帮助。 谢谢!
答案 0 :(得分:1)
你可以通过
在getView
方法中激活视图中某些元素的监听器
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;
}
}
答案 1 :(得分:0)
您应该能够将该侦听器放入onCreate方法,并将其附加到ListView。尝试这样的事情,让我们知道结果:
<?php
if(isset($_POST['send'])){
$from = $_POST['femail'];
$phoneno = $_POST['phoneno'];
$message = $_POST['message'];
$carrier = $_POST['carrier'];
if(empty($from)){
echo("enter the email");
exit();
}
else if(empty($phoneno)){
echo("enter the phone no");
exit();
}
elseif(empty($carrier)){
echo("enter the specific carrier");
exit();
}
else if(empty($message)){
echo("enter the message");
exit();
}
else{
$message = wordwrap($message, 70);
$header = $from;
$subject = 'from submission';
$to = $phoneno.'@'.$carrier;
$result = mail($to, $subject, $message, $header);
echo("message sent to".$to);
}
}
?>