我有存储在 R.drawable 文件中的图像列表。当用户单击图像列表中的一个图像时,我想共享图像。我有以下显示图像列表和排序顺序。但是当我点击一张图片时,分享意图不起作用。
我的代码:
public class MainActivity extends Activity {
EditText edittext;
ListView listview;
ImageView imageview;
String[] text = { "AbdulKalam",
"AIGross",
"AlbertAbrahamMichelson",
"AlbertEinstein",
"AlbertHofmann",
"AlbrechtvonHaller",
"AlessandroVolta",
"AlexanderCalder "};
int[] image={R.drawable.abdul_kalam,
R.drawable.ai_gross,
R.drawable.albert_abraham_michelson,
R.drawable.albert_einstein,
R.drawable. albert_hofmann,
R.drawable. albrecht_von_haller,
R.drawable. alessandro_volta,
R.drawable. alexander_calder,
R.drawable. alexander_fleming};
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext = (EditText) findViewById(R.id.EditText01);
final ListView listview = (ListView) findViewById(R.id.ListView01);
imageview=(ImageView)findViewById(R.id.ImageView01);
listview.setAdapter(new MyCustomAdapter(text, image));
edittext.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
textlength = edittext.getText().length();
text_sort.clear();
image_sort.clear();
for (int i = 0; i < text.length; i++)
{
if (textlength <= text[i].length())
{
if (edittext.getText().toString().
equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
{
text_sort.add(text[i]);
image_sort.add(image[i]);
}
}
}
listview.setAdapter(new MyCustomAdapter
(text_sort, image_sort));
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long rowId) {
// TODO Auto-generated method stub
finalStringTouch=listview.getItemAtPosition(position).toString();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra("image", Touch);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
});
}
});
}
class MyCustomAdapter extends BaseAdapter
{
String[] data_text;
int[] data_image;
MyCustomAdapter()
{
}
MyCustomAdapter(String[] text, int[] image)
{
data_text = text;
data_image = image;
}
MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image)
{
data_text = new String[text.size()];
data_image = new int[image.size()];
for(int i=0;i<text.size();i++)
{
data_text[i] = text.get(i);
data_image[i] = image.get(i);
}
}
public int getCount()
{
return data_text.length;
}
public String getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listview, parent, false);
TextView textview = (TextView) row.findViewById(R.id.TextView01);
ImageView imageview = (ImageView) row
.findViewById(R.id.ImageView01);
textview.setText(data_text[position]);
imageview.setImageResource(data_image[position]);
return (row);
}
}