我创建了一个名为popuplistfragment.java和list_view.xml的listview。我在xml文件中添加了textview,但我不知道在listview中添加代码来更改textview。
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class PopupListFragment extends ListFragment implements OnItemClickListener {
TextView textview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_item, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.quotes, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
String item = (String) parent.getItemAtPosition(position);
Log.v("check", "item is clicked");
Intent myIntent = new Intent(getActivity(), SwipeViews.class);
myIntent.putExtra("key", item);
myIntent.putExtra("index", position);
startActivity(myIntent);
}
}
这是list_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeight"
android:background="@drawable/back_listview">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="false"
>
</ListView>
<TextView
android:id="@+id/text1"
android:textColor="#e15258"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:maxLines="1"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
答案 0 :(得分:0)
首先,您需要扩展Adapter类
public class ListAdapter extends ArrayAdapter<String> {
String [] mQuotes;
public ListAdapter(String [] quotes){
mQuotes = quotes;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
v = layoutInflater.inflate(R.layout.itemlistrow, null);
}
TextView quoteTextView = v.findViewById(R.id.quote_textView);
quoteTextView.setText(mQuotes[position])
return v;
}
@Override
public int getCount() {
return quotes.length;
}
}
然后实现其他方法。 下一步是更改下一个代码
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String [] QuoteArray = getResources().getStringArray(R.array.quotes);
ListAdapter adapter = new ListAdapter(QuoteArray);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
然后将itemlistrow.xml添加到Layout文件夹并进行更改:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/quote_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
您可以根据需要自定义itemlistrow.xml