下午好,
我正在尝试触摸listView中的listItem并检测触摸(并获取触摸的listItem),因为之后我将显示一个包含帖子信息的新活动。
但是我遇到了问题,因为我不知道该怎么做,而且我尝试了很多不同的教程和代码,而且它在我的情况下从不工作,这就是为什么我要发布我的完整代码因为我现在没有选择。
这是我的代码:
public class Blog extends AppCompatActivity {
// URL
String url = "https://www.mywebsite.com/";
// Key JSON
String keyJSON = "posts";
// Construct the data source
ArrayList<Blog> array = new ArrayList<Blog>();
// Background color action bar
String actionBarBackgroundColor = "#00b7bb";
// Title action bar
String actionBarTitle = "Blog";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blog);
// Action Bar
setupActionBar(actionBarBackgroundColor, actionBarTitle);
// Create the adapter to convert the array to views
final BlogAdapter adapter = new BlogAdapter(this, array);
// Load posts
initBlog(adapter);
}
// Action Bar
public void setupActionBar(String color, String titulo) {
// Create Action bar
ActionBar mActionBar = getSupportActionBar();
// Display title
getSupportActionBar().setTitle(titulo);
// Action bar background
mActionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(color)));
// Show back arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
// Get the information and display it in the ListView
private void initBlog(final BlogAdapter adapter) {
// List View
ListView listView = (ListView) findViewById(R.id.listv);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Log.d("Click","Click");
}
});
// Attach the adapter to a ListView
listView.setAdapter(adapter);
JsonObjectRequest jsObjRequest = new JsonObjectRequest (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONObject jsonResponse = new JSONObject(response.toString());
//Show result
//Log.d("Result", jsonResponse.toString());
JSONArray jsonMainNode = jsonResponse.optJSONArray(keyJSON);
// Create products
for(int i = 0; i<jsonMainNode.length();i++){
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String titulo = jsonChildNode.getString("title");
String fecha = jsonChildNode.getString("fecha");
String imagen = jsonChildNode.getString("imagen");
String introtext = jsonChildNode.getString("introtext");
String fulltext = jsonChildNode.getString("fulltext");
Blog newPost = new Blog(titulo, fecha, imagen, introtext, fulltext);
adapter.add(newPost);
}
}
catch(JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.toString());
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
}
// Back arrow action
public boolean onOptionsItemSelected(MenuItem item){
Intent myIntent = new Intent(getApplicationContext(), MenuPrincipal.class);
startActivityForResult(myIntent, 0);
return true;
}
}
列表视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp"
tools:context=".Blog.Blog"
>
<ListView
android:id="@+id/listv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10px">
</ListView>
列出项目视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:layout_gravity="center_vertical|center_horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="right">
<TextView
android:id="@+id/blog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:textColor="@color/colorBlack"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textStyle="bold"
android:padding="0px" />
<TextView
android:id="@+id/blog_fecha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="0dp"
android:textColor="@color/colorBlack"
android:layout_marginBottom="0dp"
android:textStyle="italic" />
<ImageView
android:id="@+id/blog_image"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_width="match_parent"
android:layout_height="350px" />
<TextView
android:id="@+id/blog_intro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:textColor="@color/colorBlack"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:lineSpacingExtra="4sp" />
<Button
android:text="Ver post"
android:layout_width="wrap_content"
android:id="@+id/button"
android:textAppearance="@style/TextAppearance.AppCompat"
android:background="@color/color"
android:textColor="@color/blanco"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingLeft="20px"
android:paddingRight="20px"
android:layout_marginTop="20px" />
</LinearLayout>
</LinearLayout>
BlogAdapter:
public class BlogAdapter extends ArrayAdapter<Blog> {
public BlogAdapter(Context context, ArrayList<Blog> blogs) {
super(context, 0, blogs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Blog blog = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.blog_item, parent, false);
}
TextView blogTitulo = (TextView) convertView.findViewById(R.id.blog_title);
TextView blogFecha = (TextView) convertView.findViewById(R.id.blog_fecha);
ImageView blogImagen = (ImageView) convertView.findViewById(R.id.blog_image);
TextView blogIntrotext = (TextView) convertView.findViewById(R.id.blog_intro);
blogTitulo.setText(blog.titulo);
blogFecha.setText(blog.fecha);
blogIntrotext.setText(blog.introtext);
Picasso.with(getContext()).load(blog.imagen).into(blogImagen);
return convertView;
}
}
提前致谢,
此致
答案 0 :(得分:1)
首先使您的类Parcelable或Serializable 您需要将
ItemClickListener
添加到listview
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(Blog.this,ActivityToOpen.class);
intent.putParcelable("BlogModeKey",array.get(position));
startActivity(intent);
}
});
并在ActivityToOpen中读取该对象。
注意强> 或者使用putExtra
逐个发送每个Blog模型属性并且不要为模型类和活动使用相同的名称
答案 1 :(得分:1)
yourListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//Write code
}
});
答案 2 :(得分:1)
提取Array
响应时,您必须添加Blog
JSON
,然后使用Adapter
初始化Array
。然后将Adapter
设置为ListView
。
下面,
for(int i = 0; i < jsonMainNode.length(); i++){
......
Blog newPost = new Blog(titulo, fecha, imagen, introtext, fulltext);
array.add(newPost);
}
然后,
if(array.size() > 0){
adapter = new BlogAdapter(this, array);
// Attach the adapter to a ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Blog blog = array[position];//You have the Blog Object here
}
});
}
答案 3 :(得分:1)
要通过单击listitem启动新活动,请为ListView设置onClickListener并从此处启动一个活动,如下所示:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Blog blog= (Blog ) adapterView.getItemAtPosition(position);
Intent intent = new Intent();
// use intent.putExtras() for die kind of information you want to deliver
getApplicationContext.startActivity(intent,yourActivity.class)
}
});