gpt4 book ai didi

java - 如何在android recyclerview中获取点击项目的文本

转载 作者:行者123 更新时间:2023-11-29 07:31:27 25 4
gpt4 key购买 nike

我正在开发一个应用程序,使用 volley、fragment 和 recyclerview 显示包含文本和图像的项目列表。列表显示成功。但我想知道如何实现点击我想在列表中获取名称的产品(ImageTitleNameView 在我的代码中表示)。下面显示了我的代码。

friend fragment .java

public class FriendsFragment extends android.support.v4.app.Fragment {



List<GetDataAdapter> GetDataAdapter1;

RecyclerView recyclerView;

RecyclerView.LayoutManager recyclerViewlayoutManager;

RecyclerView.Adapter recyclerViewadapter;

String GET_JSON_DATA_HTTP_URL = "http://192.168.xx.x/1.php";
String JSON_IMAGE_TITLE_NAME = "image_title";
String JSON_IMAGE_URL = "image_url";

JsonArrayRequest jsonArrayRequest ;

RequestQueue requestQueue ;

public FriendsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_friends, container, false);


GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview1);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(recyclerViewlayoutManager);






JSON_DATA_WEB_CALL();



// Inflate the layout for this fragment
return rootView;
}

public void JSON_DATA_WEB_CALL(){

jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {

Log.d("LOGTAG", "Response :"+response);

JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});

requestQueue = Volley.newRequestQueue(getContext());

requestQueue.add(jsonArrayRequest);
}


public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

for(int i = 0; i<array.length(); i++) {

GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

JSONObject json = null;
try {

json = array.getJSONObject(i);

GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));

GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));

} catch (JSONException e) {

e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}

recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, getContext());

recyclerView.setAdapter(recyclerViewadapter);

}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}

@Override
public void onDetach() {
super.onDetach();
}
}

获取数据适配器.java

public class GetDataAdapter {

public String ImageServerUrl;
public String ImageTitleName;

public String getImageServerUrl() {
return ImageServerUrl;
}

public void setImageServerUrl(String imageServerUrl) {
this.ImageServerUrl = imageServerUrl;
}

public String getImageTitleName() {
return ImageTitleName;
}

public void setImageTitleNamee(String Imagetitlename) {
this.ImageTitleName = Imagetitlename;
}

}

sample 架.java

public class SampleHolder extends RecyclerView.ViewHolder {
public SampleHolder(View itemView) {
super(itemView);
}
}

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;

List<GetDataAdapter> getDataAdapter;

ImageLoader imageLoader1;

private final View.OnClickListener mOnClickListener = new MyOnClickListener();


public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
v.setOnClickListener(mOnClickListener);
ViewHolder viewHolder = new ViewHolder(v);

return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {

GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);

Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
Log.d("LOGTAG", "Name :"+getDataAdapter1.getImageTitleName());

imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);

Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);

Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());

}

@Override
public int getItemCount() {

return getDataAdapter.size();
}

class ViewHolder extends RecyclerView.ViewHolder{



public TextView ImageTitleNameView;
public NetworkImageView networkImageView ;

public ViewHolder(View itemView) {

super(itemView);

ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item) ;

networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;

}
}

private class MyOnClickListener implements View.OnClickListener {

@Override
public void onClick(View v) {


//Log.d("LOGTAG", "clicked : name "+ );

}
}
}

ServerImageParseAdapter.java

public class ServerImageParseAdapter {

public static ServerImageParseAdapter SIAdapter;

public static Context context1;

public RequestQueue requestQueue1;

public ImageLoader Imageloader1;

public Cache cache1 ;

public Network networkOBJ ;

LruCache<String, Bitmap> LRUCACHE = new LruCache<String, Bitmap>(30);

private ServerImageParseAdapter(Context context) {

this.context1 = context;

this.requestQueue1 = RQ();

Imageloader1 = new ImageLoader(requestQueue1, new ImageLoader.ImageCache() {

@Override
public Bitmap getBitmap(String URL) {

return LRUCACHE.get(URL);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {

LRUCACHE.put(url, bitmap);
}
});
}

public ImageLoader getImageLoader() {

return Imageloader1;
}

public static ServerImageParseAdapter getInstance(Context SynchronizedContext) {

if (SIAdapter == null) {

SIAdapter = new ServerImageParseAdapter(SynchronizedContext);
}
return SIAdapter;
}

public RequestQueue RQ() {

if (requestQueue1 == null) {

cache1 = new DiskBasedCache(context1.getCacheDir());

networkOBJ = new BasicNetwork(new HurlStack());

requestQueue1 = new RequestQueue(cache1, networkOBJ);

requestQueue1.start();
}
return requestQueue1;
}
}

最佳答案

i want to know how to implement for on clicking the product i want to get the name in list

因为点击监听器设置为recyclerview布局,而其他View都是recyclerviewView的子View。 onClick 方法的参数是 被点击的 View 的实例。

使用 v 访问点击行 TextView 数据:

@Override
public void onClick(View v) {
TextView textView = (TextView) v.findViewById(R.id.textView_item) ;
Log.d("LOGTAG", "clicked : name "+textView.getText().toString() );
}

关于java - 如何在android recyclerview中获取点击项目的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41530159/

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