更新:使用Android应用程序上的图像获取ListView的Toast

时间:2017-02-20 23:05:21

标签: android listview android-arrayadapter android-toast

对于下面的内容,我添加了以下吐司:

public void Toast (View v) {
    Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
}

它有效,但显然它所做的就是为所有东西显示“测试”。我以为我可以做'this.dogTitles'或'this.dogDescriptions',但它不会让我。

有人可以解释一下原因吗?我把它放在了onCreate方法中。

由于

无论何时点击列表项(行),我都无法解决如何编程的问题。

我可以为简单程序进行祝酒,但由于数组适配器,我正在努力解决这个问题。

我的代码如下。

提前致谢。

package lab.mad.cct.c3375331task2;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Task2Activity extends AppCompatActivity {

ListView list;
String [] dogTitles;
String [] dogDescriptions;
int[] images = {R.drawable.husky, R.drawable.pug, R.drawable.yorkshire_terrier, R.drawable.french_bulldog,
R.drawable.border_collie, R.drawable.great_dane, R.drawable.staffordshire_bull_terrier, R.drawable.golden_retriever,
R.drawable.german_shepherd, R.drawable.doberman};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_task2);

    Resources res = getResources();
    dogTitles = res.getStringArray(R.array.titles);
    dogDescriptions = res.getStringArray(R.array.descriptions);

    list = (ListView) findViewById(R.id.listView);
    dogAdapter adapter = new dogAdapter(this, dogTitles, images, dogDescriptions);
    list.setAdapter(adapter);
}
}

class dogAdapter extends ArrayAdapter<String>
{
    Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
dogAdapter(Context c, String[] titles, int imgs[], String[] desc)
{
    super(c, R.layout.single_row, R.id.largeText, titles);
    this.context = c;
    this.images = imgs;
    this.titleArray = titles;
    this.descriptionArray = desc;
}

class MyViewHolder
{
    ImageView myImage;
    TextView myTitle;
    TextView myDescription;
    MyViewHolder(View v)
    {
        myImage = (ImageView) v.findViewById(R.id.imageView);
        myTitle = (TextView) v.findViewById(R.id.largeText);
        myDescription = (TextView) v.findViewById(R.id.smallText);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    MyViewHolder holder = null;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.single_row, parent, false);
        holder = new MyViewHolder(row);
        row.setTag(holder);
        Log.d("dogs", "Creating a new row");
            }
    else {
        holder = (MyViewHolder) row.getTag();
        Log.d("dogs", "Recycling stuff");
    }
    //ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
    //TextView myTitle = (TextView) row.findViewById(R.id.largeText);
    //TextView myDescription = (TextView) row.findViewById(R.id.smallText);

    holder.myImage.setImageResource(images[position]);
    holder.myTitle.setText(titleArray[position]);
    holder.myDescription.setText(descriptionArray[position]);

    row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast toast = new Toast(getContext());
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();
        }
    });

    return row;
}
}

5 个答案:

答案 0 :(得分:0)

在设置适配器后,在onCreate内部放置一个OnClickListener,从列表开始。

list.setOnClickListener

然后在onClick中添加一个像往常一样的Toast。

如果你想从行中的特定小部件中使用它,那么使用该小部件与列表相同。但是,在适配器中执行此操作。在holder.myDescription.setText

之后执行
textView.setOnClickListener

并在其onCreate上放置你的吐司。

答案 1 :(得分:0)

通过在ArrayAdapter的getView()方法中执行以下操作,您将能够专门处理每一行的祝酒词

row.setOnClicklistener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
      //display toast here  
   }
});

答案 2 :(得分:0)

在onCreate中添加OnItemClickListener到您的ListViewOnIemClickListener内置于ListView中,以提供与ListView中每个行项的单击交互。

onCreate内:

list = (ListView) findViewById(R.id.listView);
dogAdapter adapter = new dogAdapter(this, dogTitles, images, dogDescriptions);
list.setAdapter(adapter);

list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){

        // Use the clicked item's position to get the corresponding values
        // from arrays, adapter, etc.
        Toast.makeText(getContext(), dogTitles[position] + " " + dogDescriptions[position], Toast.LENGTH_LONG)
               .show();
    }
}); 

答案 3 :(得分:0)

row.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();
    }
});

答案 4 :(得分:0)

您尚未将任何视图分配给Toast您的点击工作正常但未显示toast。 显示吐司的简单方法

Toast.makeText(this, "Android Simple Toast", Toast.LENGTH_LONG).show();

根据您的代码,您必须将一个视图设置为像这样的烤面包 toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#DAAA" >

<TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF" />

然后点击

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                               (ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);//this you missed
toast.show();